I'm reading some files and I need to remove all the apostrophes in the strings except the ones in words ending with 's.
Examples:
Frank's blue car -> Frank's blue car
Frank 'blue' car -> Frank blue car
'Frank's blue car -> Frank's blue car
Frank's' blue 'car -> Frank's blue car
'Frank blue car' -> Frank blue car
Frank'blue'car -> Frankbluecar
Thanks in advance for your help!
EDIT: Modified question since I just need the words ending with 's to keep the apostrophe and not only the singular possessive ones.
So basically you want to disallow apostrophes that aren't surrounded by letters on both sides. You can try the following:
/(?<![a-z])'|'(?!s\b)/i
Replace anything that matches that with an empty string. If you want to allow some contractions also, you can broaden it a bit:
/(?<![a-z])'|'(?![a-z]\b)/i
This pattern matches your requirements, but I offer some notes of caution:
'tis and 'twas, which are valid English.didn't.Frank's) and contractions ending in S (e.g. she's). the kids' toys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With