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 8013119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:32:45+00:00 2026-06-04T19:32:45+00:00

I am trying to implement a regexp that, given a string, it checks for

  • 0

I am trying to implement a regexp that, given a string, it checks for a sequence of at least 3 of identical characters and replaces it with two of that character. For example, I want to turn the below string:

sstttttrrrrrrriing

into

ssttrriing 

I am thinking of something along the lines of…

$string =~ s/(\D{3,})/substr($1, 0, 2)/e;

But this will not work because:

  1. It doesn’t check if the three alphabetical characters are identical; it can match a sequence of three or more distinct characters.
  2. It only replaces the first match; I need to accommodate for all matches in this regexp.

Can anyone help me?

  • 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-04T19:32:46+00:00Added an answer on June 4, 2026 at 7:32 pm

    You can use a capture group and backreference it with \1, then insert it twice afterwards.

    $ perl -plwe 's/(.)\1{2,}/$1$1/g'
    sstttttrrrrrrriing
    ssttrriing
    

    Or you can use the \K (keep) escape sequence to avoid having to re-insert.

    s/(.)\1\K\1+//g
    

    Replace wildcard . for any suitable character (class) if needed. For example for letters:

    perl -plwe 's/(\pL)\1\K\1+//g'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement AJAX with shebang - meaning that example.com/#!/somepage.php is simply example.com/somepage.php
In trying to implement the Google Analytics SDK for iOS, I've run into two
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
when trying to implement an Aspect, that is responsible for catching and logging a
I am trying to implement a regular expression to allow only one or two
I'm having problems about a regexp. I'm trying to implement a regex to select
I am trying to implement string unescaping with Python regex and backreferences, and it
I am currently trying implement a QThread that contains a socket connection. The socket
I'm trying implement a way to recursively template using jsRender. The issue is, my
I have this weird kind of error. I am trying implement basic Euclidean algorithm

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.