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?
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*)>
/regex/i
, you can also do /(?i)regex/
/first(?i)second(?-i)third/
/first(?i:second)third/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With