2

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?

noah
  • 2,418
  • 7
  • 21
Sonu Nagar
  • 21
  • 2
  • (From a deleted answer) See also https://stackoverflow.com/a/875978/11097997 – tripleee Oct 02 '20 at 04:42
  • Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – jalazbe Oct 02 '20 at 06:23
  • 1
    [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) has nothing to do with the current question. – Wiktor Stribiżew Oct 02 '20 at 07:19

2 Answers2

1

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))
Ryszard Czech
  • 10,599
  • 2
  • 12
  • 31
0

Here are two options. The first uses regex, and the second uses the string replace method.

import re
mystr = "Python's Programming: is very easy to learn"
reg = r"'s"
print(re.sub(reg, '', mystr))
   # prints: Python Programming: is very easy to learn
print(mystr.replace("'s",''))
   # prints: Python Programming: is very easy to learn
noah
  • 2,418
  • 7
  • 21
  • Replacing `'s` without checking word boundaries can cause side effects. Try with `'such an awesome language'` string. – Ryszard Czech Oct 01 '20 at 21:14
  • I'm actually not sure what you say is true. Seems to work fine without word boundaries on your string. Unless you mean ```mystr = "'such an awesome language'"```. But this seems highly unlikely – noah Oct 01 '20 at 21:23
  • I have removed the word boundaries. I don't think they are necessary in this case. – noah Oct 01 '20 at 21:49
  • And didn't intend to "copy" your answer. I just saw a comment on my answer and addressed it. Hadn't looked at your answer. – noah Oct 01 '20 at 21:52