cappie013
cappie013

Reputation: 2444

Complex regex single quote replace

I have a set of strings for which I would like to replace single quotes by double quotes. But, sometimes the single quote to replace is at the end of the line, sometimes the single quote should be replaced since it follow a S for possessive.

Example :

The song 'Miss you' is featured in The Rolling Stones' album 'Voodoo Lounge'

should be

The song "Miss you" is featured in The Rolling Stones' album "Voodoo Lounge"

Thanks your help :)

Upvotes: 0

Views: 54

Answers (1)

Amal Murali
Amal Murali

Reputation: 76656

Regular expressions can only deal with raw text. It can't tell context or grammar. So it is pretty much impossible to build up a regular expression that will correctly identify the occurrences of non-possessive s characters.

However, if you'd like to ignore such cases, and match rest of them, you can use the following regex with lookaround assertions:

(?<!s)'(?!s\b)

Note that this will not match for valid cases like Blurred Lines, Dangerous etc.

Working demo

Upvotes: 1

Related Questions