Tamara
Tamara

Reputation: 2980

PHP regexp: caseless modifier for part of the expression

I'm wondering if it's possible in PHP to set regexp modifier for just a part of the expression (not whole expression).

I have the following code:

    $romanDigitsRegexp="(M{1,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|M{0,4}(CM|C?D|D?C{1,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|M{0,4}(CM|CD|D?C{0,3})(XC|X?L|L?X{1,3})(IX|IV|V?I{0,3})|M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|I?V|V?I{1,3}))";

    $test = "Properties of Wide Bandgap li-vi Semiconductors VolUme1";

    $volumeInfoFound = preg_match("/(\bv|Volume|\bvol)[\.| ]*(\d+|".$romanDigitsRegexp.")\b/i",$test, $matches);

This global /i modifier causes problem with roman digits. I would like to remove it but at the same time catch such words like "volume","VOLUME","vol","VOL", "Vol". Is it possible to add \i modifier just for this part:

(\bv|Volume|\bvol)

Upvotes: 2

Views: 76

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89567

You can switch on/off modifiers like this:

(?i)subpattern(?-i)

note: the action of an inline modifier is limited to the innermost group. So ((?i)toto)abcd doesn't match totoABCD. And a group doesn't protect against a modifier of an upper level: (?i)(toto)abcd matches TOTOabcd.

if you want to switch on a modifier for a subpattern enclosed in a non-capturing group you can use this syntax too:

(?i:subpattern)

The same to switch off:

(?-i:subpattern)

Note: unfortunately you can't do the same with an atomic group. (You can't write (?i>subpattern))

For the two syntaxes you can switch several modifiers at the same time:

(?ix-m)

(?ix-ms:subpattern)

Note: it isn't forbidden to write (?i-i), but it's useless.

Upvotes: 3

Related Questions