如何检测字符串中的笑词? [英] How can I detect laughing words in a string?

查看:49
本文介绍了如何检测字符串中的笑词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在字符串中检测诸如hahahaha"和lolololol"之类的笑词.

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])(.*?)$

但是,这不符合我的目的.它有效,但它也匹配与笑声完全无关的词,例如kill",因为它只查找包含字母 l、o、h、a 的任何词.

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.

如何检测字符串中的笑词(如hahaha"或lololol")?

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

推荐答案

试试这个模式:

\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

这篇关于如何检测字符串中的笑词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆