I want to check if string contains punctuation or not so a continuous sequence of exclamation, question & both. By continuous, it means more than 2 times. Just like below,
#If sentence contains !!!
exc = re.compile(r"(.)\!{2}")
word["cont_exclamation"] = if exc.search(sent[i]) else not(found)
#If sentence contains ???
reg = re.compile(r"(.)\?{2}")
word["cont_question"] = if reg.search(sent[i]) else not(found)
But now I want to find both, exclamation and question marks so for example, hello??! or hey!! or dude!?!
Also, what if I want ? and ! both but more than 2 any of them.
I dont know regex properly so any help would be great.
edit: Typing "regex tutorial" into google gives more info than you possibly need. This tutorial looks particularly well-balanced between conciseness and completeness.
Particularly (i.m.o.) useful tricks that are often not mentioned:
use
+?
and*?
to switch from greedy to lazy match. I.e. match as few characters as possible instead of as much as possible. Example text:#ab# #de#
-->#.*?#
matches#ab#
only (not#ab# #de#
)parentheses create a capture group by default. If you don't want this, you can use
(?:
...)
.Most importantly, comment each regexp with a human-readable explanation. Future-you will be grateful. :-)