So basically we need a regex to strip excess punctuation from a string, leaving only one of the punctuation characters.
So:
This is my awesome string!!!!! Don’t you love it???!!??!!
Would result in
This is my awesome string! Don’t you love it?!
I have tried and tried and tried to get this, but I either end up mangling the string or it doesn’t work at all. I’m still learning Regexes so please forgive what is surely a stupid question.
I guess “punctuation” would be pretty much anything that’s not A-Za-z0-9
Edit It appears that I misunderstood our original requirements. Using the accepted solution below, how would I adjust it so that no matter what characters you have, the punctuation is limited to the first only?
IE
???!!!!!!
would become just
?
And
This is my string!!!?!?!?!? Isn’t it great???!?!?!!
would become
This is my string! Isn’t it great?
Similar to the other answers, but should take care of any non
0-9a-zA-Zcharacters in any order leaving you with one of each left:Should turn
into
It works by using a positive lookahead to see if the character appears again in this string of punctuation. If it does, it’s replaced with the empty string.