Regex matching uppercase characters with lowercase search

[亡魂溺海] 提交于 2019-12-10 20:50:45

问题


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 from a to f and A to F, and then again will match a lowercase letter
  • S(?i:[a-z])S - S or s will be matched with S (depends on the environment settings like Match case), then any upper- or lowercase letter and then S/s.


来源:https://stackoverflow.com/questions/25383504/regex-matching-uppercase-characters-with-lowercase-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!