Can you make just part of a regex case-insensitive?

I've seen great deals of instances of making a whole normal expression instance - aloof. What I'm questioning is having simply component of the expression be instance - aloof.

As an example, allow is claim I have a string similar to this:


What if I intend to match all events of "foo" no matter instance yet I just intend to match the upper - instance "BAR"s?

The excellent remedy would certainly be something that functions throughout regex tastes yet I'm interested in listening to language - details ones too (Thanks Espo)


The link Espo gave was really handy. There is an example in there concerning transforming modifiers on and also off within the expression.

For my contrived instance, I can do something similar to this:


that makes the suit instance - aloof for simply the foo section of the suit.

That appeared to operate in the majority of regex executions other than Javascript, Python, and also a couple of others (as Espo stated).

The large ones that I was questioning (Perl, PHP,. NET) all assistance inline setting adjustments.

2022-06-08 04:52:15
Source Share
Answers: 4

Perl allows you make component of your normal expression instance - aloof by utilizing the (? i pattern modifier.

Modern regex tastes permit you to use modifiers to only component of the normal expression. If you insert the modifier (? ism) in the center of the regex, the modifier just relates to the component of the regex to the right of the modifier. You can switch off settings by preceding them with a minus indicator. All settings after the minus indicator will certainly be switched off, Eg. (? i - sm) activates instance ignorance, and also switches off both solitary - line setting and also multi - line setting.

Not all regex tastes sustain this. JavaScript and also Python use all setting modifiers to the whole normal expression. They do not sustain the (? - ismx) syntax, given that switching off an alternative is meaningless when setting modifiers relate to the entire normal expressions. All alternatives are off by default.

You can promptly examine just how the regex taste you are making use of takes care of setting modifiers. The regex (? i) te (? - i) st needs to match examination and also TEst, yet not examination or TEST.


2022-06-08 05:02:21

You could make use of


The?: in the brackets in.Net suggests it is non - recording, and also simply made use of to organize the regards to the|(or) declaration.

2022-06-08 04:55:08

What language are you making use of? A typical means to do this would certainly be something like/ ([Ff] [Oo] 2 |BAR)/ with instance level of sensitivity on, yet in Java, as an example, there is an instance level of sensitivity modifier (? i) that makes all personalities to the right of it instance aloof and also (? - i) which compels level of sensitivity. An instance of that Java regex modifier can be located here.

2022-06-08 04:55:01

Unfortunately syntax for instance - aloof matching is not usual. In.NET you can make use of RegexOptions.IgnoreCase flag or ? i modifier

2022-06-08 04:54:59