Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to get data from list with regular expressions

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
0
0
US
Hello all,
I have a problem, I am getting a generated list of computers type, OS's, and computer names. The problem is that I am unable to parse the information via tabs or white space because of how the list is generated. I want to use a regular expression to get the 3 pieces of data that are being separated from white space. Here is some of the data:
Code:
PC               Windows2003      mv12
PC               Windows2003      mnf02
Solaris          Solaris9         meta1
Solaris          Solaris9         mta2
PC               Windows2003      vdc1
I have many servers to do this too so I am not sure if I can get the same number of white spaces to do a split with. I am wondering if I could do a regular expression like [A-Za-z0-9] and grab the 3 pieces of information that are being returned within every line. Can this be done and if so, then how. I am under the gun on this one, so Thanks for the help.

timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
Assuming your data is in a string variable called "strData" and each line is separated by a linefeed and each value per line is separated by a tab:

first split the string by each line

Code:
raEachLine = split(strdata,vbNewLine)

next go through each line and split it by the tab
Code:
for ndx = 0 to ubound(raEachLine) - 1
raThisLine = split(raEachLine(ndx),vbTab)
' check below for final comment
next

You're array in each itteration of the loop will look like this
raThisLine(0) = firstValue
raThisLine(1) = secondValue
raThisLine(2) = thirdValue

 
sorry - didn't read the part that said you can't get the same number of spaces...in that case it will be a little tougher - I'll see if I can figure something out for you, maybe someone else will be able to do it quicker

I hate writing reg expressions :)
 
ok, it doesn't use Regular Expressions, but I am going to assume that you'll have at least 3 spaces between each value in strData and that each individual value for the columns won't have any more than 2 spaces.

Code:
raEachLine = split(strData,vbNewLine)
for ndx = 0 to ubound(raEachLine) - 1
	' this is spliting each line by 3 spaces, change it if you need to.  
	raTemp = split(raEachLine(ndx),"   ")
	str = ""
	for i = 0 to ubound(raTemp)		
		if raTemp(i)<> "" then 
			if str <> "" then str = str & vbTab
			str = str & raTemp(i) 
		end if
	next
	raThisLine = split(str,vbTab) ' SEE BELOW FOR EXPLANATION

next

For each itteration:
raThisLine(0) = 1st Value
raThisLine(1) = 2nd Value
raThisLine(2) = 3rd Value
 
Here's a RegExp solution that doesn't need to know how many white space characters there are between the fields ...


Private Function GetData(strSource As String) As String()
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\s+"
GetData = Split(.Replace(strSource, " "), " ")
End With
End Function
 
Sorry, that was the VB version. Here is the VBScript version

Private Function GetData(strSource)
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\s+"
GetData = Split(.Replace(strSource, " "), " ")
End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top