0

I have this sentence that I need to parse. The senetence is the following bellow:

I'm sure _I_ shan't be able!  I shall be a great deal too far off to trouble myself

The outcome that I need below is :

I'm sure I shan't be able!  I shall be a great deal too far off to trouble myself

What would the Exrepssion look like?

The current expression I am using is

r'(?!_)\w+' 

but the outcome I get is:

I'm sure I_ shan't be able!  I shall be a great deal too far off to trouble myself

Any suggestions would be appreciated thank you!

2 Answers2

0

I guess you want something like this.

text = "I'm sure _I_ shan't be able!  I shall be a great deal too far off to trouble myself"

print(text.replace('_',''))
#expected output:
#I'm sure I shan't be able!  I shall be a great deal too far off to trouble myself
JEX
  • 124
  • 1
  • 6
0

You can remove all underscores around a word with:

import re

text = "I'm sure _I_ shan't be able! I shall be a great _deal_ too far off to _trouble_ myself"

result = re.sub(r'(_)(\w+)(_)', r'\2', text)

print(result)  # I'm sure I shan't be able! I shall be a great deal too far off to trouble myself
Darius
  • 10,762
  • 2
  • 29
  • 50