Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: remove 's from a string?

Tags:

python

regex

string input: Python's Programming: is very easy to learn

expected output: Python Programming: is very easy to learn

Here is what I have so far which isn't working:

import re
mystr = "Python's Programming: is very easy to learn"
reg = r'\w+'
print(re.findall(reg, mystr))

How do I remove the 's from python's?

like image 228
Sonu Nagar Avatar asked Aug 14 '26 09:08

Sonu Nagar


1 Answers

You extract all matches of one or more alphanumeric characters.

Use

\b's\b

See proof.

Explanation:

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  's                       '\'s'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

Python code:

import re
mystr = "Python's Programming: is very easy to learn"
print(re.sub(r"\b's\b", '', mystr))
like image 77
Ryszard Czech Avatar answered Aug 16 '26 23:08

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!