SunAns
SunAns

Reputation: 313

remove empty quotes from string using regex

I want to remove punctuation such as " ", ' ', , , "", '' from my string using regex. The code so far I've written only removes the ones which space between them. How do I remove the empty ones such as '',

#Code
s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
s = re.sub("' '|` `|" "|""|''|``","",s)
print(s)

My expected outcome:

hey how is the what are you doing how is everything

Upvotes: 1

Views: 670

Answers (2)

sacuL
sacuL

Reputation: 51335

In this case, why not match all word characters, and then join them?

' '.join(re.findall('\w+',s))
# 'hey how is the what are you doing how is everything'

Upvotes: 1

anubhava
anubhava

Reputation: 785098

You may use this regex to match all such quotes:

r'([\'"`])\s*\1\s*'

Code:

>>> s = "hey how ' ' is the ` ` what are '' you doing `` how is everything"
>>> print (re.sub(r'([\'"`])\s*\1\s*', '', s))
hey how is the what are you doing how is everything

RegEx Details:

  • ([\'"`]): Match one of the given quotes and capture it in group #1
  • \s*: Match 0 or more whitespaces
  • \1: Using back-reference of group #1 make sure we match same closing quote
  • \s*: Match 0 or more whitespaces

RegEx Demo

Upvotes: 4

Related Questions