Get consecutive capitalized words using regex

前端 未结 3 1421
孤街浪徒
孤街浪徒 2020-12-15 09:51

I am having trouble with my regex for capturing consecutive capitalized words. Here is what I want the regex to capture:

\"said Polly Pocket and the toys\" -         


        
3条回答
  •  隐瞒了意图╮
    2020-12-15 10:27

    Use a positive look-ahead:

    ([A-Z][a-z]+(?=\s[A-Z])(?:\s[A-Z][a-z]+)+)
    

    Assert that the current word, to be accepted, needs to be followed by another word with a capital letter in it. Broken down:

    (                # begin capture
      [A-Z]            # one uppercase letter  \ First Word
      [a-z]+           # 1+ lowercase letters  /
      (?=\s[A-Z])      # must have a space and uppercase letter following it
      (?:                # non-capturing group
        \s               # space
        [A-Z]            # uppercase letter   \ Additional Word(s)
        [a-z]+           # lowercase letter   /
      )+              # group can be repeated (more words)
    )               #end capture
    

提交回复
热议问题