Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case sensitive and insensitive in the same pattern

Thanks to the help with my previous homework question Regex to match tags like <A>, <BB>, <CCC> but not <ABC>, but now I have another homework question.

I need to match tags like <LOL>, <LOLOLOL> (3 uppercase letters, with repeatable last two letters), but not <lol> (need to be uppercase).

Using the technique from the previous homework, I tried <[A-Z]([A-Z][A-Z])\1*>. This works, except there's an additional catch: the repeating part can be in mixed case!

So I need to also match <LOLolol>, <LOLOLOlol>, because it's 3 uppercase letters, with repeatable last two letters in mixed case. I know you can make a pattern case-insensitive with /i, and that will let me match <LOLolol> with the regex I have, but it will also now match <lololol>, because the check for the first 3 letters are also case-insensitive.

So how do I do this? How can I check the first 3 letters case sensitively, and then the rest of the letters case-insensitively? Is this possible with regex?

like image 570
regex4html Avatar asked Jun 25 '10 10:06

regex4html


1 Answers

Yes! You can in fact do this in some flavors, using what is called embedded modifier. This puts the modifier in the pattern, and you can essentially select which parts of the pattern the modifiers apply to.

The embedded modifier for case insensitivity is (?i), so the pattern you want in this case is:

<[A-Z]([A-Z]{2})(?i:\1*)>

References

  • regular-expressions.info/Modifiers
    • Specifying Modes Inside The Regular Expression
      • Instead of /regex/i, you can also do /(?i)regex/
    • Turning Modes On and Off for Only Part of The Regular Expression
      • You can also do /first(?i)second(?-i)third/
    • Modifier Spans
      • You can also do /first(?i:second)third/
like image 68
polygenelubricants Avatar answered Nov 03 '22 21:11

polygenelubricants