PCRE Case Sensitivity
Previous Microplanet Gravity Next |
Gravity's PCREs are NOT case sensitive by default. To use case sensitive matching, use the " i " PCRE internal option to turn off (unset) the caseless default setting:
(?-i)
Set Case for Entire String
Use the option at the start of the string to set case matching for the entire string:
(?-i)Gravity
Matches "Gravity" only (an upper case "G" followed by lower case "ravity"). Case is enforced in the entire string.
Does not match "GRAVITY" or "gravity"
Match Case in the Middle of the String
^(Re:)?.*(?-i)Gravity
Matches an optional "Re:" (any case: RE:, Re:, re:), at the beginning of the string followed by case sensitive matching for the remainder of the string.
Matches
I find Gravity case Re: I find Gravity case RE: I find Gravity case
Does not match
I find GRAVITY case I find gravity case
Match Case in the Middle of the String
You can turn case on and off (unset/set the internal caseless option):
(?-i) sets case (unsets caseless option) (?i) set caseless (resets caseless option)
The PCRE
(?-i) Gra (?i) Vity
Matches
Gravity GraViTY
Does not match
GRAVITY gravity GrAvItY
Another way
From the PCRE Manual: An option change within a subpattern affects only that part of the current pattern that follows it.
The following PCRE matches the same as above, using a subpattern
((?-i)Gra)Vity
Multiple Unset/Set
(?-i)G(?i)ra(?-i)V(?i)ity
Matches
GRAVITY GraViTY
Does Not Match
Gravity GrAvItY gravity
The same thing with subpatterns:
((?-i)G)ra((?-i)V)ity
POSIX Expressions (advanced)
POSIX expressions work in character classes (square brackets), so the expression would look like this [[:lower:]]
However, you will also need to set case sensitivity to use POSIX expressions that involve case.
We will try to match a Subject that is all CAPITALS and may also have digits or punctuation. In other words, the subject has no lower case letters in the entire string.
Correct Way:
^(?-i)[^[:lower:]]+$
Wrong Way:
^[^[:lower:]]+$