Max
Max

Reputation: 1094

Java - Replace single quote without affecting multi single quotes

Is it possible to replace a single quote ' without affecting multi-single quotes (e.g. ''').

For example, I want to replace ' with '''

GIVEN --> EXPECT
-------------------------------------------
"text" --> "text"
"long'text" --> "long'''text"
"long'long''text" --> "long'''long''text" 
"long'long'''text" --> "long'''long'''text" 
"long'long'text" --> "long'''long'''text" 

Thanks in advance

Upvotes: 1

Views: 101

Answers (1)

anubhava
anubhava

Reputation: 784998

For matching use this look-around based regex:

(?<!')'(?!')

and replace it by:

'''

RegEx Demo

(?<!')'(?!') matches a single quote if is not followed and preceded by a single quote.

Upvotes: 3

Related Questions