Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:09:43+00:00 2026-06-10T16:09:43+00:00

So basically we need a regex to strip excess punctuation from a string, leaving

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:09:45+00:00Added an answer on June 10, 2026 at 4:09 pm

    Similar to the other answers, but should take care of any non 0-9a-zA-Z characters in any order leaving you with one of each left:

    $newstring= preg_replace('/([^\w\s])(?=[^\w\s]*\1)/', '', $oldstring);
    

    Should turn

    This is my awesome string!!!!! Don't you love it???!!??!!
    

    into

    This is my awesome string! Don't you love it?!
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I need a regex which will return true if the string is a
I need a regex that checks if a string only contain letters(a-z) and that
I have a string like this: blah blah blah_0123 So basically i need a
I basically need a custom function to be used only when, for example, the
I basically need to check if there is an easier way to do this
Basically I need to capture the video from videocamera do some processing on frames
I have unfortunately wandered into a situation where I need regex using Ruby. Basically
i need a regex for: 123,456,789,123,4444,.. . basically comma separated values. The INT part
I need to have a RegEx that will match a URI like this based
I'm writing validation regex in c# - basically need to ensure that property does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.