Regex for non-consecutive upper-case words

后端 未结 5 1072
抹茶落季
抹茶落季 2021-01-14 17:15

Here\'s one for you Regex wizards.

This is for use within Notepad++, i.e. it is entered directly into the search and replace dialog.

I need

相关标签:
5条回答
  • 2021-01-14 17:27

    You can use this pattern:

    (?:[^A-Z]\s+|[^a-zA-Z\s]|\A)\K[A-Z]+\b(?!\s+[A-Z])
    
    0 讨论(0)
  • 2021-01-14 17:39

    I'd use

    (?<![[:upper:]]) ([[:upper:]]+) (?!([[:upper:]]| )+)
    

    this will also select anything your locale considers to be uppercase, not just A-Z

    0 讨论(0)
  • 2021-01-14 17:42

    I tried that regex and it works for me when I use the 'Mark' feature of the Notepad++ Find/Replace. I'm not sure if you have any other test cases though.

    (?:[[:lower:]][^A-Za-z]*|\A)\K\b[[:upper:]]+\b(?=[^A-Za-z]*[[:lower:]]|\Z)
    

    I'm using \A and \Z because you mentioned multilines.

    regex101 demo (with some explanations there).

    Be sure to check 'Match case'.

    Before: enter image description here

    After: enter image description here

    EDIT: As per update, to inverse the highlighting, I came up with that one... a bit lengthy:

    [^A-Z]+|(?:(?=[A-Z]*[a-z])\b[A-Za-z]+\b)|(?:\b[A-Z]+\b(?=[^a-z]+[A-Z])(?:[^a-z]*[A-Z]+)*)
    

    regex101 demo

    enter image description here

    0 讨论(0)
  • 2021-01-14 17:45

    I would try

    (^|\.\s+|[a-z]\s+)([A-Z]+)(\.|\s+[a-z]|$)
    

    That matches: a period or start-of-line or end of a lowercase word; followed by an uppercase word; followed by a period, end-of-line or start of a lowercase word.

    The word itself is matched in group 2. If Notepad++ supports lookaround assertions, you can do this so that the only captured word is the single capitalized word:

    (?:^|\.\s+|[a-z]\s+)([A-Z]+)(?:\.|\s+[a-z]|$)
    
    0 讨论(0)
  • 2021-01-14 17:48

    You should check for surrounding characters with negative lookahead and negative lookbehind:

    (?<![A-Z]\s)\b[A-Z]+\b(?!\s[A-Z])
    

    Live demo

    Notepad++ v6.5.1 & works on multi-line purposes too

    Notepad++ v6.5.1

    0 讨论(0)
提交回复
热议问题