user16085212
user16085212

Reputation:

remove special character inside words with regex

i have text like this : some text 'elle m'a dit ce qu'elle voulait.<break></break>' some text

I want the ' to be ignored when it's not between words

Result wanted : some text elle m'a dit ce qu'elle voulait.<break></break> some text

I came up with this regex code : \b(')\b, but it's doing the opposite work and i get on output:

some text 'elle ma dit ce quelle voulait.<break></break>' some text

Upvotes: 1

Views: 55

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627409

You can use

r"\B'|'\B"

See the regex demo. It matches a ' that is not enclosed with word boundaries on both ends.

Details:

  • \B - a position other than a word boundary position (as the next pattern is ', the \B means there must be start of string or any non-word char immediately to the left of the current location
  • ' - a straight apostrophe
  • | - or
  • ' - an apostrophe
  • \B - that is either immediately followed with a non-word char or at the end of string.

See the Python demo:

import re
text = "some text 'elle m'a dit ce qu'elle voulait.<break></break>' some text"
print( re.sub(r"\B'|'\B", '', text) )
# => some text elle m'a dit ce qu'elle voulait.<break></break> some text

Upvotes: 1

Related Questions