Reputation: 397
Say I have the string:
"foo bar foo x foo y bar foo"
I want to find words occurring three or more times, so "foo".
To find a word occuring more than once I have the regex
\b(\w+)\b(?=.*\1)
I tried (\b(\w+)\b(?=.*\1)){3,}
, but this returns no matches. Any suggestions?
Thanks.
Upvotes: 1
Views: 410
Reputation: 785246
You can use this regex:
^.*(\b\w+\b)(?<!^.*\1.*\1)(?:(?:(?!\1).)*\1){3}(?:(?!\1).)*$
Explanation:
^.*
: Match from the start.(\b\w+\b)
: Match and capture a word in group #1(?<!^.*\1.*\1)
: Lookbehind to assert no group #1 between the start and group #1
(?:(?:(?!\1).)*\1){3}
: Match something plus group #1 three times
(?:(?!\1).)*
: Match anything except group#1(?:(?!\1).)*$
: Match anything except group#1 to the endUpvotes: 4