I am having issues with a regular expression in my app. I am trying to test a string which can take one of three forms:
1 - any integer
2 - any integer followed by and number of lower case letters
3 - a valid roman numeral
I have defined the regex as follows:
dim validExpression as string ="(\b^[0-9]+[a-z]+$\b)|(\b[ivxlcdmIVXLCDM]+\b)|(\b[0-9]+\b)" // valid string types
This is supposed to say: any integer followed by and number of lower case letters OR a valid roman numeral OR any integer
I have a function as follows:
Function doesStringMatchRegex(regex as String, testString as String) As Boolean
Dim valid as String
Dim r as new Regex
dim rm as regExMatch
r.SearchPattern = regex
rm = r.Search(testString)
while rm <> nil
valid = rm.subExpressionString(0)
Exit
wend
if valid <> "" Then
return true
Else
return false
End If
End Function
All my test cases pass except that when i pass 123F (in violation of the first part of my expression, the string passes validation and it should not.
I have tried any number of permutations but without success. Can someone show me the error of my ways?
[quote=149298:@Kem Tekinay]I prefer to start the pattern with a switch rather than using .Options. This makes the pattern transportable and clearer to the reader.
[/quote]
… clearer to you maybe
I was once in the computing service building at the University of Cambridge, and I passed by the office of Philip Hazel, who wrote the PCRE library, which php (and others) use to implement regular expressions. I wasn’t sure whether to knock and thank him, or knock and harangue him for the pain and suffering which regexes had caused me. In the end I just walked on by…