regex

  • Thread starter Thread starter jg
  • Start date Start date
J

jg

I made a mistake somewhere in my vb code and I look, check and read against
the articles and help on regex, I still can't find the mistake I made.

I know my test string and the test patterns works, because I used on a vs.
script to check. I also believe I foolwed followed the regex syntax for
dotnet.

here is the source code for the function and testing

Public Function regtest(ByVal StringIn As String, ByVal patrn As
String) As Integer
' Create a new Regex object.
Dim r As New Regex(patrn)
' Find a single match in the input string.
Dim m As Match = r.Match(StringIn)


If m.Success Then
' Print out the character position where a match was found.
' (Character position 3 in this case.)
' Console.WriteLine("Found match at position " &
m.Index.ToString())
regtest = m.Index
else
Return -1
End If
End Function


The values for StringIn is "Mon, 2005-Feb-13 and some stuff"

for patrn is

"(\b([1-3][0-37-9]){0,1}[0-9][0-9][ -](([JFMASOND][AEPUCO][NBRYNLGPTVC]([A-Z]{0,6}))|([01]{0,1}[0-9]))[
-][01]{0,1}[0-9])"

The function regtest should return a number of 5 , at least not minus 1
which is what I got

Your help spotting the error is greatly appreciated.

Thank you for your time.
 
Hi jg,

My 'only' comment would be whether you have commented out the line
'm.Index.ToString' and this is part of the commented previous line!

Otherwise, your code looks good and should return your string.......assuming
it is actually found. Just note that I don't know whether the match method
is case sensitive. Not sure if this helps.

Good luck, Phil
 
case sensitivity was part of the problem which I fixed with
RegexOptions.IgnoreCase
However there is still problem with my regex pattern,
I could test with simple one like "FEB", or
"((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OCT)|(NOV)|(DEC))"
and get the right answer but not
'([ADFJMNOS][ACEOPU][BCGLNPRTVY])"
In other word I got syntax problem with the month pattern

Phil said:
Hi jg,

My 'only' comment would be whether you have commented out the line
'm.Index.ToString' and this is part of the commented previous line!

Otherwise, your code looks good and should return your
string.......assuming it is actually found. Just note that I don't know
whether the match method is case sensitive. Not sure if this helps.

Good luck, Phil

jg said:
I made a mistake somewhere in my vb code and I look, check and read
against the articles and help on regex, I still can't find the mistake I
made.

I know my test string and the test patterns works, because I used on a
vs. script to check. I also believe I foolwed followed the regex syntax
for dotnet.

here is the source code for the function and testing

Public Function regtest(ByVal StringIn As String, ByVal patrn As
String) As Integer
' Create a new Regex object.
Dim r As New Regex(patrn)
' Find a single match in the input string.
Dim m As Match = r.Match(StringIn)


If m.Success Then
' Print out the character position where a match was found.
' (Character position 3 in this case.)
' Console.WriteLine("Found match at position " &
m.Index.ToString())
regtest = m.Index
else
Return -1
End If
End Function


The values for StringIn is "Mon, 2005-Feb-13 and some stuff"

for patrn is

"(\b([1-3][0-37-9]){0,1}[0-9][0-9][ -](([JFMASOND][AEPUCO][NBRYNLGPTVC]([A-Z]{0,6}))|([01]{0,1}[0-9]))[
-][01]{0,1}[0-9])"

The function regtest should return a number of 5 , at least not minus 1
which is what I got

Your help spotting the error is greatly appreciated.

Thank you for your time.
 
One last 'suggestion' from me then, try this:-

Public Function regtest(ByVal StringIn As String, ByVal patrn As
String) As Integer
' Create a new Regex object.
Dim newPtrn as string = Trim(patrn)
Dim r As New Regex(newPtrn)

Good luck. Phil

jg said:
case sensitivity was part of the problem which I fixed with
RegexOptions.IgnoreCase
However there is still problem with my regex pattern,
I could test with simple one like "FEB", or

"((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OCT)|(NOV)|(DEC))"
and get the right answer but not
'([ADFJMNOS][ACEOPU][BCGLNPRTVY])"
In other word I got syntax problem with the month pattern

Phil said:
Hi jg,

My 'only' comment would be whether you have commented out the line
'm.Index.ToString' and this is part of the commented previous line!

Otherwise, your code looks good and should return your
string.......assuming it is actually found. Just note that I don't know
whether the match method is case sensitive. Not sure if this helps.

Good luck, Phil

jg said:
I made a mistake somewhere in my vb code and I look, check and read
against the articles and help on regex, I still can't find the mistake I
made.

I know my test string and the test patterns works, because I used on a
vs. script to check. I also believe I foolwed followed the regex syntax
for dotnet.

here is the source code for the function and testing

Public Function regtest(ByVal StringIn As String, ByVal patrn As
String) As Integer
' Create a new Regex object.
Dim r As New Regex(patrn)
' Find a single match in the input string.
Dim m As Match = r.Match(StringIn)


If m.Success Then
' Print out the character position where a match was found.
' (Character position 3 in this case.)
' Console.WriteLine("Found match at position " &
m.Index.ToString())
regtest = m.Index
else
Return -1
End If
End Function


The values for StringIn is "Mon, 2005-Feb-13 and some stuff"

for patrn is

"(\b([1-3][0-37-9]){0,1}[0-9][0-9][ -](([JFMASOND][AEPUCO][NBRYNLGPTVC]([A-Z]{0,6}))|([01]{0,1}[0-9]))[
-][01]{0,1}[0-9])"

The function regtest should return a number of 5 , at least not minus 1
which is what I got

Your help spotting the error is greatly appreciated.

Thank you for your time.
 
Back
Top