I'm reading some files and I need to remove all the apostrophes in the strings except the ones in words ending with 's.


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.


Only ' characters or also ’ characters?

Written by WTP

Only ' -Thanks!!

Written by Shaz

can you precize your language (php, C#, java ...) please.

Written by Stephan

I specified the language (php) but @nick removed it from the tags.

Written by Shaz

@Stephan, why do we need language? I removed the PHP tag because the question made no mention of it being important. If you feel I made the wrong decision please, by all means, add the PHP tag.

Written by Nick

@Nick the language indicates which flavour is in use, since regex varies from flavour to flavour. Some flavour may accept some extensions when others don't.

Written by Stephan

Accepted Answer

So basically you want to disallow apostrophes that aren't surrounded by letters on both sides. You can try the following:


Replace anything that matches that with an empty string.

If you want to allow some contractions also, you can replace the s above with another [a-z]. Note that this will also block things like 'tis and 'twas, though.

Note, also, that without an English dictionary database, it will be impossible to distinguish between singular-possessive and contractions ending in S such as "he's" and "it's".

Written by Justin Morgan
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki