Watsup with my simple RegExp?

J

Jeannot

Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures 13 inches ")
Dim s As String = mc.Groups.Item(0).ToString()

s is " 13 " with a leading and a trailing space, and driving me nuts.
My set of parenthesis clearly specify only digits, where do the spaces
come from???.

What am I doing wrong????

VS2008 .net 3.5
 
T

Tom Shelton

Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures        13      inches ")
Dim s As String = mc.Groups.Item(0).ToString()

s is " 13 " with a leading and a trailing space, and driving me nuts.
My set of parenthesis clearly specify only digits, where do the spaces
come from???.

What am I doing wrong????

well... the primary match is " 13 " because of the \s so, if you look
at group item 1, it will be 13:

mc.Groups.Item(1).Value

In other words, the expression matches the entire expression, the
parens are a sub match. You could just write it like this:

"\d+"

Dim s As String = "Puppy measures 13 12 inches "
Dim matches As MatchCollection = Regex.Matches(s, "\d+")

For Each m As Match In matches
Console.WriteLine(m.Value)
Next

HTH
 
E

eBob.com

Jeannot said:
Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures 13 inches ")
Dim s As String = mc.Groups.Item(0).ToString()

s is " 13 " with a leading and a trailing space, and driving me nuts.
My set of parenthesis clearly specify only digits, where do the spaces
come from???.

What am I doing wrong????

VS2008 .net 3.5

There is always a Group 0 which corresponds to the entire pattern. And of
course your entire pattern does include white space characters. Try the
following:

Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures 13 inches ")
Dim s As String = mc.Groups.Item(0).ToString()
MsgBox("s is """ & s & """; Length of s is " & s.Length.ToString)

s = mc.Groups.Item(1).ToString()
MsgBox("s is """ & s & """; Length of s is " & s.Length.ToString)

Good Luck, Bob
 
J

Jeannot

Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures 13 inches ")
Dim s As String = mc.Groups.Item(0).ToString()
s is " 13 " with a leading and a trailing space, and driving me nuts.
My set of parenthesis clearly specify only digits, where do the spaces
come from???.
What am I doing wrong????
VS2008 .net 3.5

There is always a Group 0 which corresponds to the entire pattern. And of
course your entire pattern does include white space characters. Try the
following:

Dim re As New Regex("\s([0-9]+)\s")
Dim mc As Match = re.Match("Puppy measures 13 inches ")
Dim s As String = mc.Groups.Item(0).ToString()
MsgBox("s is """ & s & """; Length of s is " & s.Length.ToString)

s = mc.Groups.Item(1).ToString()
MsgBox("s is """ & s & """; Length of s is " & s.Length.ToString)

Good Luck, Bob

That was it.
Thanks Guys!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top