Help with regular expression

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?

Thanks,
Thomas

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.

(?-i)

Thanks - I’ll try that…

[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 :slight_smile:

So picky.

me ?
always :slight_smile:

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.

H

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:

\\b(?=[IVXLCDM]+\\b)(?'millenia'M*)(?'century'CM|DC{0,3}|CD|C{1,3})?(?'decade'XC|LX{0,3}|XL|X{1,3})?(?'year'IX|VI{0,3}|IV|I{1,3})?\\b

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 :slight_smile:

Kem sneezing punctuation all over again ?
Really needs to get that looked into :slight_smile:

You so jelly.

Concord grape jelly :stuck_out_tongue:

I don’t think you’re ready for THIS jelly…