Watsup with my simple RegExp?

  • Thread starter Thread starter Jeannot
  • Start date Start date
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
 
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
 
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
 
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!
 
Back
Top