Reputation: 13
I want to remove certain words or characters from a sentence with some exceptions using regular expression.
For example- I have a string this is [/.] a string [ra] with [/] something
, I want to remove [ra]
, [/.]
but not [/]
.
I used:
m = re.sub('\[.*?\]','',n)
which works fine, how can I retain this-> [/]
Upvotes: 1
Views: 69
Reputation: 163362
You could use an alternation to capture in a group what you want to keep and match what you want to remove.
result = re.sub(r"(\[/])|\[[^]]+\]", r"\1", n)
Explanation
(\[/])|\[[^]]+\]
(\[/])
Capture [/]
in a group|
Or\[[^]]+\]
Match an opening square bracket until a closing square bracket using a negated character classReplace with the first capturing group \1
Upvotes: 1
Reputation: 37377
Use this pattern \[(?!\/\])[^\]]+\]
and replace all matches with empty string.
Explanation: it matches [
with \[
, then it assures, that what follows is NOT \]
, so we don't match [\]
, it's done with negative lookahead: (?!\/\])
, then it matches everything until ]
and ]
itself with pattern [^\]]+\]
([^\]]+
matches one or more characters other then ]
).
Upvotes: 1
Reputation: 626929
You may use
re.sub(r'\[(?!/])[^][]*]', '', n)
See the regex demo.
Details
\[
- a [
char(?!/])
- a negative lookahead that fails the match if there is /]
immediately to the right of the current location[^][]*
- 0+ chars other than [
and ]
]
- a ]
char.Upvotes: 2