Thursday, March 21, 2013

Pattern Matching in Excel/VBA/Regex

So posting here for posterity and in case I need to come back to this at a later date:

Originally posted here: http://stackoverflow.com/questions/15554132/pattern-match-count-in-excel-regex-vba

I was posed with a problem yesterday and could find no solution anywhere, I was about to post a help request here when I figured out a way to meet the needs of my customer. It isn't pretty, it isn't fast, but it works, customer is happy, and I've suggested we visit how they generate their data rather than indefinitely use this fix. Since I could find no solution anywhere, I will post it here in hopes it will help someone else out.
Office 2007 XLSX file containing over 5000 records like the below (a single cell with multiple rows of text). The issue, on adjacent cell, put count of incidents from the cell. Looking at the cell data for A1 you can see 3 incidents.

Cell A1

1/15/2013 1:30:11 AM Userx Had to reboot system  
1/15/2013 1:32:11 AM Userx System running finished rebooting and appears to be working  
11/15/2013 12:30:11 AM Userx System hung again

The problem is that I'm no regular expression guru and well the date value isn't consistent. The day month and hour can be single or double digit, but their always noted on a new line.
My code solution was to take the cell, split it on line break, trim everything 5 characters after the last ':' and evaluate the the result against my regular expression. After that, some basic tally and text insert to adjacent cell. I am sure this can be improved and that there maybe plugins/addons for Excel that would have made this a lot easier, but solution needed to use the built in formulas or vba of Excel
The below is an example of how the function would be called.
'calling function from another source:
...
thecount = CountOfDateValues(Range("a1").Value) 'get count
Range("b1").Value = thecount 'put count to adjacent cell
...
Below is the VBA code of a function that takes in a string value and returns count of matches to the regular expression. I hope it is of use for someone.
Function CountOfDateValues(thetext)

Dim data() As String 
Dim yourInput As String
yourInput = thetext 
Dim TheSplitter As String
TheSplitter = Chr(10) 'the character that represents a line break

data = Split(yourInput, TheSplitter ) ' creates an array of strings for each line in the cell
Dim re
Set re = CreateObject("VBscript.regexp")
'regular expression that matches ##/##/#### ##:##:## ##
re.Pattern = "(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$"
re.Global = True

Dim t As String
Dim theCount As Integer
theCount = 0
For i = LBound(data) To UBound(data) 'from first item in array to last item in array

        For Each Match In re.Execute(Left(data(i), InStrRev(data(i), ":") + 5))
            'from start of string to 5 characters past the last ':' of string
            theCount = theCount + 1
        Next
    Next

CountOfDateValues = theCount 

End Function
Referencing urls:
MS Access 2003 VBA String Split on Line Break
http://sourceforge.net/projects/regexbuilder/files/regexbuilder/1.4.0/
This tool made testing my regular expression against various date formats remarkably easy.
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryid=5&p=2
I was able to save a lot of time crafting a regular expression by using a precrafted one from here. Sadly did not learn much by do1ing so, but I believe I saved a lot of time on this 'we need it done now' request.
*Note: There is a window for a false positives if someone starts their worklog note with a timetamp, I noted this to the customer and they were fine with it.

Monday, October 1, 2012

'Settings' is not a member of 'My'

Okay, so my project has been beating me over the head with this:
'Settings' is not a member of 'My'
webconfig was properly built.
tested other projects worked fine as the code was copied from them

So I was have a big wtf at this problem, as a generic error I was not finding any solution.
I found one post where the user created a new page in the same project, copied everything over and it worked, so I tried that, and it worked... here's the rundown of what I did....
 I am using Visual Studio 2012.

  • default.aspx - bad
  • default.aspx.vb - bad
  • default1.aspx - new
  • default1.aspx.vb - new
  1. blanked out the contents of both bad files
  2. copy pasted contents(code view of aspx file) of both to new files
  3. deleted  bad files via solution explorer
  4. renamed new files to bad file names (ie removed the 1 from the file name)
  5. my.settings references are working along with several other reference errors I was going to look at once i fixed the my.settings error.
or so I thought it was fixed...on compile it just blew up again on me

so I created a whole new  project. replicated steps above to transfer code, worked out several items that broke but were obvious fixes, and viola, 'my' errors went away.
Truth be told i had tried this before, but I think i had done File>New>Website... this last time round, I did File>New>Project

Friday, June 8, 2012

Found a nice url that formats code snippets into formatted text for blogger: http://codeformatter.blogspot.com/

Network Scanner

Okay, i have achieved a part of my dreams in coding today. I've always wanted to write nifty little network tools that serve a purpose, today, I have actually written one, well at least the draft of a proof of concept for a larger project. Two key reference that I used to generate the code was: http://www.xtremevbtalk.com/showthread.php?t=321718
1:  Imports System  
2:  Imports SnmpSharpNet  
3:  Public Class Form1  
4:    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click  
5:      'check if keyed fields contain numeric only data,stop if they don't  
6:      If Not IsNumeric(Me.tb_f_o1.Text) Then Exit Sub  
7:      If Not IsNumeric(Me.tb_f_o2.Text) Then Exit Sub  
8:      If Not IsNumeric(Me.tb_f_o3.Text) Then Exit Sub  
9:      If Not IsNumeric(Me.tb_f_o4.Text) Then Exit Sub  
10:      If Not IsNumeric(Me.tb_t_o1.Text) Then Exit Sub  
11:      If Not IsNumeric(Me.tb_t_o2.Text) Then Exit Sub  
12:      If Not IsNumeric(Me.tb_t_o3.Text) Then Exit Sub  
13:      If Not IsNumeric(Me.tb_t_o4.Text) Then Exit Sub  
14:      tb_results.Text += "start" & vbCrLf  
15:      'convert text box values to integers  
16:      Dim a_to As Integer = CInt(Me.tb_t_o1.Text)  
17:      Dim a_from As Integer = CInt(Me.tb_f_o1.Text)  
18:      Dim b_to As Integer = CInt(Me.tb_t_o2.Text)  
19:      Dim b_from As Integer = CInt(Me.tb_f_o2.Text)  
20:      Dim c_to As Integer = CInt(Me.tb_t_o3.Text)  
21:      Dim c_from As Integer = CInt(Me.tb_f_o3.Text)  
22:      Dim d_to As Integer = CInt(Me.tb_t_o4.Text)  
23:      Dim d_from As Integer = CInt(Me.tb_f_o4.Text)  
24:      ' Use Parse() to convert from the integers to an IPAddress.  
25:      Dim fromAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(a_from & "." & b_from & "." & c_from & "." & d_from)  
26:      Dim toAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(a_to & "." & b_to & "." & c_to & "." & d_to)  
27:      ' We need the integral value of the IP. The Address property is deprecated. Use GetAddressBytes() and  
28:      ' convert the bytes to a value. If you are using IPv4, this should be 4 bytes, 32 bits, only Integer is needed.  
29:      Dim currentAddress As System.Net.IPAddress  
30:      Dim addressBytes() As Byte = fromAddress.GetAddressBytes()  
31:      Dim addressValue As Integer = BitConverter.ToInt32(addressBytes, 0)  
32:      Dim toaddressBytes() As Byte = toAddress.GetAddressBytes()  
33:      Dim toaddressValue As Integer = BitConverter.ToInt32(toaddressBytes, 0)  
34:      ' The bytes come out in network order; we don't want to figure out if that's backwards or not. Use this method  
35:      ' to convert to whatever the host machine uses.  
36:      addressValue = System.Net.IPAddress.NetworkToHostOrder(addressValue)  
37:      toaddressValue = System.Net.IPAddress.NetworkToHostOrder(toaddressValue)  
38:      For i = addressValue To toaddressValue  
39:        Dim thei As Integer = i  
40:        Dim theb() As Byte  
41:        ' We're ready to go back to bytes; convert the number back to network order  
42:        thei = System.Net.IPAddress.HostToNetworkOrder(thei)  
43:        ' Turn it back into bytes, create the new IP  
44:        theb = BitConverter.GetBytes(thei)  
45:        currentAddress = New System.Net.IPAddress(theb)  
46:        Dim thehostname As String  
47:        Try  
48:          thehostname = System.Net.Dns.GetHostEntry(currentAddress.ToString).HostName.ToString  
49:        Catch ex As Exception  
50:          thehostname = "Could not detect hostname"  
51:        End Try  
52:        tb_results.Text += " IP: " & currentAddress.ToString & " | Active: " & pinger(currentAddress.ToString) & " | Hostname: " & thehostname & vbCrLf  
53:        My.Application.DoEvents()  
54:      Next  
55:      tb_results.Text += "done" & vbCrLf  
56:    End Sub  
57:    Function pinger(ip As String)  
58:      If My.Computer.Network.Ping(ip) Then  
59:        Return (1)  
60:      Else  
61:        Return (0)  
62:      End If  
63:    End Function  

I have a dream... about my future tablet.

These two urls showed the power that can be found in current windows 8 tablets. I would ditch my ipad for this in a heartbeat. As a software developer, being able to have the ability to work with my native developer app (Visual Studio) and be able to play Diablo 3 ( a game that just came out last month, and that I plan to play for several years) as well as having full blown versions of all the applications I normally use rather than ‘apps’ appeals greatly to me. http://www.drdobbs.com/mobile/232900072?vXFf&cid=sem_edit_DDJ&wc=4&vQZf https://www.youtube.com/watch?v=ukn3wp3Dr-Q Ideally, I would want to be able to take my tablet, dock it at home, and be able to plug at least 3 monitors into the dock. Secondly, I would hope the dock would have a more powerful gpu, cpu, and possibly ram that pairs with the tablets hardware to provide more of a work horse when docked. So that's my tablet dream for the day...

Thursday, March 29, 2012

890FXA-GD70 gives me FF code and doesn't post

So bought the parts for a new build.
Was done over the course of 3 months, so by time was ready for assembly return windows had closed.
System would power, fans would spin for a second, motherboard diag leds would show FF

PartsL
890FXA-GD70
FX-8120
Ram - 4gb sticks 1600 cl7 or 4gb sticks 1600 cl9 sticks

Contacted support, issue was bios version, mobo had old version.
Asked around no one had a am3 cpu lying around, so bought one online for $30 (grr)
System came right up with that cpu, flashed bios from usb stick, put in FX 8120 and came right up.

Reading online I see alot of returns and such over this issue, i would think they could find some way to post the system for bios patch without the cpu somehow.

Found this article while troubleshooting, thought it was worth mentioning as a guide for troubleshooting your hardware's install.
http://www.tomshardware.com/forum/261145-31-perform-steps-posting-post-boot-video-problems

Tuesday, July 20, 2010

IPAD vs Win7 vs Comcast 2go INet

Okay so here's a headache and once again I'm left blaming comcast.
I try for 2 days to share my usb Comcast 2go's internet with my ipad via an adhoc wireless network on my win7 laptop.

I could get connected to the laptop and ping it (grabbed an iphone app that lets you ping addresses) but could not get out to ping google or browse the web.

So came along a post today about Connectify, a little virtual router that would give better control than an ad hoc network. Several hours of tinkering, nothing, Connectify says there is internet access, but cannot reach anything/ping anything beyond the laptop. Even at some points the laptop lost inet.

My best guess is the comcast software you have to have running to connect/stay online.

Since Connectify was letting comcast bully it and not get it's fair share of the connection i tried another one. http://virtualrouter.codeplex.com/ Virtual Router.
Installed, asked me to name the network, give a password, and which inet connection to share. Set my Ipad to the new network and boom off and running and pinging google.