The Third
The Third

Reputation: 795

replace punctuation with nothing

>>> import re
>>> a="what is. your. name? It's good"
>>> b=re.findall(r'\w+',a)
>>> b
['what', 'is', 'your', 'name', 'It', 's', 'good']

The above results in splitting It's to ['It','s'] I dont want that.

I want to just replace it with nothing i.e. It's will be Its. Same applies to all the punctuation. How can I achieve that?

Upvotes: 0

Views: 3356

Answers (2)

Deelaka
Deelaka

Reputation: 13693

To match any word character and a single quoted comma ' if there is any.:

import re
string = "Many cook's were involved and many cooked pre-season food"
punctaution = re.findall(r"\w+([\-_.!~*'()])\w+",string)

for i in punctaution:
    string = re.sub(i,'',string)

print string

Output:

Many cooks were involved and many cooked preseason food

Upvotes: 0

Abhijit
Abhijit

Reputation: 63737

Are you compelled to use regex? This task can easily ne accomplished by using str.translate and string.punctuation as the deletechars

>>> from string import punctuation
>>> a="what is. your. name? It's good"
>>> a.translate(None, punctuation)
'what is your name Its good'

If you are compelled to use regex, another option for you would be

>>> from string import punctuation
>>> r = re.compile(r'[{}]+'.format(re.escape(punctuation)))
>>> r.sub('', a)
'what is your name Its good'

But, I would still suggest you to reconsider the design. Using Regex for this task is an overkill.

Upvotes: 7

Related Questions