How can I detect laughing words in a string?

亡梦爱人 提交于 2019-12-18 05:53:04

问题


I'm trying to detect laughing words like "hahahaha" and "lolololol" in a string.

Currently I'm using the following regex:

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

However, this doesn't work for my purposes. It works, but it also matches words totally unrelated to laughter, such as 'kill', because it simply looks for any word that contains the letters l, o, h, a.

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


回答1:


try with this pattern:

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

or better if your regex flavour support atomic groups and possessive quantifiers:

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



回答2:


\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



回答3:


To keep it simple, because the solutions posted may be overly complicated for what you want to do: if the only thing you count as "laughing words" are ha, haha, etc. and lol, lolol, lololol, etc., then the following regular expression will be sufficient:

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

This assumes a regex dialect in which \b represents a word boundary, which you seem to be using.




回答4:


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



回答5:


In Python, I tried to do it in this way:

import re

re.sub(r"\b(?:a{0,2}h{1,2}a{0,2}){2,}h?\b", "<laugh>", "hahahahha! I love laughing")

>> <laugh>! I love laughing



来源:https://stackoverflow.com/questions/16453522/how-can-i-detect-laughing-words-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!