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)
if valid <> "" Then
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?
Ok - I finally found my issue. Apparently the RegEx will ignore case unless you tell it otherwise?
Is it just me or does that fly in the face of the regular expression I just wrote? le sigh
For thoise who want to know, all I did to resolve this was add the following in my function:
r.Options.CaseSensitive = true
Sorry to have bothered you all - I had made assumptions on how RegEx worked that were incorrect.
I prefer to start the pattern with a switch rather than using .Options. This makes the pattern transportable and clearer to the reader.
[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.
… clearer to you maybe
Your Roman Numeral regex isn’t really matching for valid numerals - just characters within them. For example, it’ll match the word ‘Livid’.
Have a look at http://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression - there’s a regex there for numerals up to 4k.
I don’t know if you’re allowing entries such as 0 or 0b - your regex is allowing those, too.
PS Loving the autocorrect of ‘regex’ to ‘reggae’ - I wonder what regex music would be like…
I included a Roman Numeral sample with RegExRX. Here is the pattern:
Note that the “Allow Empty Matches” option has to be set to false.
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…
Sounds like a right hook followed by a quick hug
Kem sneezing punctuation all over again ?
Really needs to get that looked into
I don’t think you’re ready for THIS jelly