iqra sadra iqra sadra - 6 months ago 29
Python Question

Python regex removing word with regex

I'm new to python and I want to remove and replace the

({ / / })
with space, the sample below

the original sentence:

NULL ({ / / }) Regina ({ 4 p1 p2 / / }) Shueller ({ 5 p1 p2 / / }) works ({ / / }) for ({ / / }) Italy ({ 14 / / }) 's ({ 15 / / }) La ({ 16 / / }) Repubblica ({ 17 / / }) newspaper ({ 18 / / }) . ({ 38 / / })


transform to this:

Regina Shueller works for Italy 's La Repubblica newspaper.


I've tried this code but that was not what I expected

Sentence = re.sub(r'[({ / / })]',' ', sentence)

Answer

You can use

r'\s*(?:\(\{[^/]*/\s*/\s*}\)|NULL)\s*'

See regex demo

Regex explanation:

  • \s* - zero or more whitespaces
  • (?:\(\{[^/]*/\s*/\s*}\)|NULL) - two alternatives, NULL or \(\{[^/]*/\s*/\s*}\) matching...
    • \( - opening round bracket
    • \{ - opening brace
    • [^/]* - zero or more characters other than /
    • / - a literal /
    • \s* - zero or more whitespaces
    • /\s* - ibid.
    • } - a closing brace
    • \) - a closing round bracket
  • \s* - zero or more whitespaces

Note that the spaces in between words and punctuation should be handled separately.

Python demo:

import re
p = r'\s*(?:\(\{[^/]*/\s*/\s*}\)|NULL)\s*'
test_str = "NULL ({ / / }) Regina ({ 4 p1 p2 / / }) Shueller ({ 5 p1 p2 / / }) works ({ / / }) for ({ / / }) Italy ({ 14 / / }) 's ({ 15 / / }) La ({ 16 / / }) Repubblica ({ 17 / / }) newspaper ({ 18 / / }) . ({ 38 / / })"
result = re.sub(p, " ", test_str)
print(result.strip())
# => Regina Shueller works for Italy 's La Repubblica newspaper .