Search code examples
regexvb.netvalidationcase-sensitivecase-insensitive

RegEx Case-Insensitive Inline


I have the following Regular Expression in VB.NET which works just fine.

(Istr_)*(8[25]\d{5}[0-9xX]{2}|DR\d{5}[0-9xX]{2}|R\d{2}-\d{4})

However, I want the "Istr_" part only to be case-insensitive, while the rest of the expression remains case-sensitive. I attempted that by simply adding "?i:" according to the MSDN documentation like so:

(?i:Istr_)*(8[25]\d{5}[0-9xX]{2}|DR\d{5}[0-9xX]{2}|R\d{2}-\d{4})

But this breaks the RegularExpressionValidator in my form.

Does the * have something to do with this? I'm not sure is the appropriate character to join the patterns. I want the first pattern to be an optional case-insensitive prefix to the second pattern.

Furthermore, I don't want to allow spaces, which I haven't been able to figure out how to do yet.

Thanks. :)


Solution

  • See the discussion here: Can you make just part of a regex case-insensitive?

    Or make a not very beautiful solution, but works:

    ([Ii][Ss][Tt][Rr]_)?(8[25]\d{5}[0-9xX]{2}|DR\d{5}[0-9xX]{2}|R\d{2}-\d{4})
    

    I've changed your * to ?. This means:

    ?: the prefix iStR_ is optional
    *: the prefix IStr_ is optional but can occur multiple times