Regex - identify final quote for escaping


Given the phrase

WHERE name='john' OR OR name='o'reilly' OR name='thomas'' OR name='o'reillys'' 

I am trying to escape

  • the middle quote of o'reilly
  • the final quote of thomas'
  • the middle and final quote of o'reillys'

to end up with something like

WHERE name='john' or name='o''reilly' OR name='thomas''' OR name='o''reillys'''

FTR, I'm using .Net

var ret = Regex.Replace(myText, myRegexPattern, "''")

The Regex

\b'\b

seems to work with the apostrophe in the middle of the word but not for the trailing apostrophe. I've had a play trying to define a regex where the word doesn't start with a ' but ends with one and I've also had a look at Lookbehind, but I'm not sure how I should achieve this.

I'd be grateful any guidance please.

Thx

S



Consider the following minor change to your Regex pattern...

\b'\b|''

Good Luck!