Silly Expressions Question

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I now know that \S means any non-whitespace, but how would i fit it into the sample code below please? I found out about the \S, but no where seems to discuss how to actually incorporate it into code.

I want to split each line of the array at each 'space' into separate words, but i dont want it to return 'space' as a results if theres a part that is 'doubled-spaced'. Which it is doing currently.


Private Sub ReadHosts()
Dim strReader As New System.IO.StreamReader("c:\windows\system32\drivers\etc\hosts_test")
Dim arrayChkdHost(1000, 3) As String
Dim arraySingleHost() As String, arrayIP$(100), arrayNS$(100), arrayOther$(100)
Dim HostLine As String
Dim i As Integer
'Clear ListBox

'Read in the hosts file line by line, breaking it down with string.split
i = 0
HostLine = strReader.ReadLine
Do While Not (HostLine Is Nothing)
arraySingleHost = HostLine.Split(" ")
arrayIP(i) = arraySingleHost(0)
arrayNS(i) = arraySingleHost(1)
arrayOther(i) = arraySingleHost(2)
MsgBox(arrayIP(i) + " = IP")
MsgBox(arrayNS(i) + " = NS")
MsgBox(arrayOther(i) + " = Other")
HostLine = strReader.ReadLine
i = i + 1
Loop

End Sub

Also in addition could someone please explain to me what the $ in : 'arrayIP$(100)', does please?

Thank you
 
Last edited:
You'll have to use regular expressions (more specifically, the RegEx class) to accomplish what you're looking for.

However, you can easily loop through your arraySingleHost and ignore all spaces.
 
How can i fit RegEx into the string.split though?, surely it would pass the information only once and if it was discarded then the data would just be missed.
 
RegEx is entirely different then string.Split(). RegEx is the class used for Regular Expressions, while string.Split is just a basic method for splitting a string.

Try searching the MSDN library and google for related tutorials on Regular Expressions (there's also one on these forums which I posted in a similar thread.. you can also search for it). You should also take a look in the MSDN library for information on string.Split.
 
I have researched them, i can find all the details about them but no real useful examples.

So are you saying that i should use RegEx instead of splitstring then? Coz i thought you was referring to using them both combined.
 
This works but it is sloppy and slow.

Add the following at the just after you open your do loop:

Do Until HostLine.IndexOf(" ") = -1
HostLine = HostLine.Replace(" ", " ")
Loop

It is not a pretty solution and you will take a performance hit but it is the best I could think of. If you get a better solution, I would like to see it.
 
Back
Top