3

Hello I need to find all sets of 2 capital words : So far I have:

([A-Z][a-z]+\s?){2}

But it does not work always. For Example with the string:

Expedition Runic Monster Markers

I expect to get :

  1. Expedition Runic
  2. Runic Monster
  3. Monster Markers

I only get 1 and 3. But I also want to get 2. I'm really stuck here. Any help is appreciated.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    Welcome! You can [capture inside a lookahead](https://stackoverflow.com/questions/11430863/how-to-find-overlapping-matches-with-a-regexp) -> [sample](https://regex101.com/r/khaY5y/2) – bobble bubble Sep 21 '22 at 02:23

1 Answers1

3

Overlapping matches need to be captured inside a lookahead.

\b(?=((?:[A-Z][a-z]+\s?\b){2}))

See this demo at Regex101 (further added \b word boundaries)

If you are on Python, there is PyPI regex with overlapped=True.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • 1
    This solved it for me and thanks for the demo resource. I'm using Java this worked as it, Thanks for the quick answer! – Felipe Gomez Sep 21 '22 at 05:50