9

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?

3
  • 2
    @Bill, when everyone understands the question one way except for you, maybe the problem isn't everyone else, but your reading comprehension.
    – Cairnarvon
    May 9, 2013 at 3:13
  • 1
    @Bill, I am sure the OP will be glad to state the opposite as he just accepted a solution wich doesn't do what you understood. Read the initial question over and over again and you will get it...
    – plalx
    May 9, 2013 at 3:13
  • 2
    Sorry, if my grammar is not perfect, my main language is not english
    – gamehelp16
    May 9, 2013 at 3:15

5 Answers 5

7

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
8
  • 1
    Why all the non-capturing groups? Regex is hard enough to read already. I would remove them.
    – Bohemian
    May 9, 2013 at 2:51
  • Don't forget to match hah, hahah, lool, loooooooll, etc
    – Patashu
    May 9, 2013 at 2:53
  • @Bohemian: Because you don't need to capture anything May 9, 2013 at 2:54
  • 1
    @Patashu, Well in that case we could also say any combination of ha or lo letters... haaahahahaah lloooooooolllllll, but at some point...
    – plalx
    May 9, 2013 at 2:56
  • @Bill, reread the question. That's why your answer got downvoted into oblivion.
    – Cairnarvon
    May 9, 2013 at 3:00
6

\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
0
6

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.

2
  • +1 This is the most effective solution. Keep things simple. This sounds like it's in the context of a game, and your players will understand how to trigger the game's laughing reaction if they want to. This doesn't have to be a flawless humour-detection AI. May 9, 2013 at 3:10
  • Often someone will type lolololololool and typo the end so it isn't a perfect repetition, though.
    – Patashu
    May 9, 2013 at 3:10
1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.