0

I'm trying to remove a certain part of a string but currently not getting the correct answer. The string is:

new_text = "'Bob' is currently 'Absent'. Hi, my name is Matt. 'Bob' is currently 'Active'. Bob: Hey Matt"

I want the two substrings: 'Bob' is currently 'Absent'. and 'Bob' is currently 'Active'. to disappear. The name "Bob" may toggle between names with alphabetical and non-alphabetical characters. "Absent" and "Active" are two of the more common statuses displayed, however, I'd like to keep track of any string inside the single quotes.

This is the final sentence I want to return: Hi, my name is Matt. Bob: Hey Matt.

Currently my code is:

re.sub(r"\'([\w\W\d]+)\' is currently \'[\w\W\d]+\'.\s", '', new_text)

And it's returning: Bob: Hey Matt

Can anyone help with this?

Agrosel
  • 489
  • 3
  • 15
  • 1
    `[\w\W\d]+` = `.+` with `re.DOTALL`. Make the quantifier lazy, `+?`. Or, use negated character class, `[^']+`. Note you do not need to escape `'` inside a double quoted string literal, and you may add `*` quantifier after `\s` to match 0 or more repetitions. – Wiktor Stribiżew May 11 '18 at 19:16
  • Thanks @WiktorStribiżew, still learning all of this stuff. Adding the `?` got what I was looking for. – Agrosel May 11 '18 at 19:19

0 Answers0