What about
\b(?=lol)\S*(\S+)(?<=\blol)\1*\b
(replace lol with omg , haha , etc.)
This will match lol , lololol , lollll , lollollol , etc., but lolo , lollllo , lolly , etc. will not work.
Rules:
- Match the word
lol all the way. - Then allow any repetition of one or more characters at the end of the word (i.e.
l , ol or lol )
So \b(?=zomg)\S*(\S+)(?<=\bzomg)\1*\b will match zomg , zomggg , zomgmgmg , zomgomgomg , etc.
In Python with comments:
result = re.sub( r"""(?ix)\b # assert position at a word boundary (?=lol) # assert that "lol" can be matched here \S* # match any number of characters except whitespace (\S+) # match at least one character (to be repeated later) (?<=\blol) # until we have reached exactly the position after the 1st "lol" \1* # then repeat the preceding character(s) any number of times \b # and ensure that we end up at another word boundary""", "lol", subject)
This will also correspond to the βunpaintedβ version (for example, lol without repetition). If you do not want this, use \1+ instead of \1* .
source share