Regex for removing single quotes from string except for possessive nouns?

Viewed 1594

I have the following regex in Java:

String regex = "[^\\s\\p{L}\\p{N}]";
Pattern p = Pattern.compile(regex);

String phrase = "Time flies: "when you're having fun!" Can't wait, 'until' next summer :)";
String delimited = p.matcher(phrase).replaceAll("");

Right now this regex removes all non-spaces and nonAlphanumerics.

Input: Time flies: "when you're having fun!" Can't wait, 'until' next summer :)
Output: Time flies when youre having fun Cant wait until next summer

Problem is, I want to maintain the single quotes on words, such as you're, can't, etc. But want to remove single quotes that are at the end of a sentence, or surround a word, such as 'hello'. This is what I want:

Input: Time flies: "when you're having fun!" Can't wait, 'until' next summer :)
Output: Time flies when you're having fun Can't wait until next summer

How can I update my current regex to be able to do this? I need to keep the \p{L} and \p{N} as it has to work for more than one language.

Thanks!

1 Answers
Related