问题
I'm using notepad++ and I'm finding that when I use regex to search for strings where I specifically want to find lowercase letters ("[a-z]") it will sometimes return uppercase letters.
I originally was searching using the string:
^[A-Z][a-z].+?$
With the purpose of deleting any line in my file that began with an uppercase character, followed by a lowercase, followed by anything until the end of the line. However, this returned lines like, "CLONE" and "DISEASE" which were only capital letters. Out of curiosity, I tried:
^[a-z].+?$
And it still returned those lines in all-caps. Finally, I tried:
^[\u0061-\u007A].+?$
And it still returned lines of all-caps text. Is there something outside of my brackets that's causing this to happen?
回答1:
As many other text editors, Notepad++ provides a global option to Match case
. Even if your expression does not contain internal modifier (?i)
the results can be unexpected depending on whether Match case
is set ON or OFF.
So, your ALLCAPS lines are valid match for ^[A-Z][a-z].+?$
because the letters are matched in a case insensitive way when Match case
is OFF.
Check Match case
to enable case sensitivity for regex search:
OTHER WAYS TO OVERRIDE CASE SENSITIVITY
There are inline flags you may use with some regex flavors to hardcode case sensitivity for all or part of the pattern:
(?-i)[A-Z][a-z]*
will only match an uppercase letter followed with lowercase ones as(?-i)
turns the case sensitivity ON(?i)[A-Z][a-z]*
will match 1 or more uppercase or lowercase letters(?-i)[a-z](?i)[a-f](?-i)[a-z]
will match a lowercase letter, then a lower- or an uppercase letter froma
tof
andA
toF
, and then again will match a lowercase letterS(?i:[a-z])S
-S
ors
will be matched withS
(depends on the environment settings likeMatch case
), then any upper- or lowercase letter and thenS
/s
.
来源:https://stackoverflow.com/questions/25383504/regex-matching-uppercase-characters-with-lowercase-search