arc
arc

Reputation: 13

How can i retain specific characters in a sentence

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

Answers (3)

The fourth bird
The fourth bird

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 class

Replace with the first capturing group \1

Regex demo

Python demo

Upvotes: 1

Michał Turczyn
Michał Turczyn

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 ]).

Demo

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions