How can I detect funny words in a string?

I am trying to find funny words like "hahahaha" and "lolololol" in a string.

I am currently using the following regex:

^((.*?)|)(\b[ha]|\b[lo])(.*?)$ 

However, this does not work for my purposes. It works, but it also matches words completely unrelated to laughter, such as “kill,” because it just searches for any word containing the letters l, o, h, a.

How can I detect funny words (like "hahaha" or "lololol") in a string?

+8
source share
4 answers

try with this template:

 \b(?:a*(?:ha)+h?|(?:l+o+)+l+)\b 

or better if your regular expression flavor supports atomic groups and possesses quantifiers:

 \b(?>a*+(?:ha)++h?|(?:l+o+)++l+)\b 
+6
source

\b(a*ha+h[ha]*|o?l+o+l+[ol]*)\b

Matches:

 hahahah haha lol loll loool looooool lolololol lolololololo ahaha aaaahahahahahaha 

Does not match:

 looo oool oooo llll ha l o lo ol ah aah aha kill lala haunt hauha louol 
+6
source

To keep it simple, because posted solutions can be overly complex for what you want to do: if you think the only thing you think is “laughing words” is ha , haha , etc. and lol , lolol , lololol , etc., the following regular expression will suffice:

 \b(ha)+|l(ol)+\b 

This suggests the regex dialect, in which \b is the word boundary that you seem to be using.

+6
source

you can try

 regex_pattern = "\b(?:a*(?:ha)+h?|h*ha+h[ha]*|(?:l+o+)+l+|o?l+o+l+[ol]*)\b" 

You can try can in this:

 sentance = hhhaaahhhaaa 
+1
source

All Articles