string consists of punctuation

772 views Asked by At

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.

3

There are 3 answers

1
Torben Klein On
regex = re.compile(r"(.)(\?|\!){2}")

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. :-)

2
Oliver Matthews On

try re.compile(r"(.)[\?\!]{2}")

3
Mark On

Use the regex '[?!]{3,}' which means match the ? or ! characters 3 or more times (if continous = more than two times). Quoting is not needed inside character class.

Add more punctuation characters to the char class as needed