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

  • Home
  • SEARCH
  • 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 1065727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:52:42+00:00 2026-05-16T19:52:42+00:00

I want to replace consecutive symbols just one such as; this is a dog???

  • 0

I want to replace consecutive symbols just one such as;

this is a dog???

to

this is a dog?

I’m using

str = re.sub("([^\s\w])(\s*\1)+", "\\1",str)

however I notice that this might replace symbols in urls that might happen in my text.

like http://example.com/this–is-a-page.html

Can someone give me some advice how to alter my regex?

  • 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-05-16T19:52:44+00:00Added an answer on May 16, 2026 at 7:52 pm

    So you want to unleash the power of regular expressions on an irregular language like HTML. First of all, search SO for “parse HTML with regex” to find out why that might not be such a good idea.

    Then consider the following: You want to replace duplicate symbols in (probably user-entered) text. You don’t want to replace them inside a URL. How can you tell what a URL is? They don’t always start with http – let’s say ars.userfriendly.org might be a URL that is followed by a longer path that contains duplicate symbols.

    Furthermore, you’ll find lots of duplicate symbols that you definitely don’t want to replace (think of nested parentheses (like this)), some of them maybe inside a <script> on the page you’re working on (||, && etc. come to mind.

    So you might come up with something like

    (?<!\b(?:ftp|http|mailto)\S+)([^\\|&/=()"'\w\s])(?:\s*\1)+
    

    which happens to work on the source code of this very page but will surely fail in other cases (for example if URLs don’t start with ftp, http or mailto). Plus, it won’t work in Python since it uses variable repetition inside lookbehind.

    All in all, you probably won’t get around parsing your HTML with a real parser, locating the body text, applying a regex to it and writing it back.

    EDIT:

    OK, you’re already working on the parsed text, but it still might contain URLs.

    Then try the following:

    result = re.sub(
        r"""(?ix) # case-insensitive, verbose regex
    
        # Either match a URL 
        # (protocol optional (if so, URL needs to start with www or ftp))
        (?P<URL>\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$])
    
        # or
        |
    
        # match repeated non-word characters
        (?P<rpt>[^\s\w])(?:\s{0,100}(?P=rpt))+""", 
    
        # and replace with both captured groups (one will always be empty)
        r"\g<URL>\g<rpt>", subject)
    

    Re-EDIT: Hm, Python chokes on the (?:\s*(?P=rpt))+ part, saying the + has nothing to repeat. Looks like a bug in Python (reproducible with (.)(\s*\1)+ whereas (.)(\s?\1)+ works)…

    Re-Re-EDIT: If I replace the * with {0,100}, then the regex compiles. But now Python complains about an unmatched group. Obviously you can’t reference a group in a replacement if it hasn’t participated in the match. I give up… 🙁

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

Sidebar

Related Questions

I Want Replace String ':' by '/', I am Using this code to replace
I want to replace each character of a string by a different one, shifted
I want to replace child elements from one tree to another , based on
I want replace default close button in Fancybox 1.3.4 with custom one. I've changed
I have this code, and I want to know, if I can replace only
I want to replace a word that is in html content (not attributes) and
I want to extract variables from math expression using c#. I wrote this code
i'v got such string <>1 <>2 <>3 i want remove all '<>' and symbols
I want to replace a lines that ends with 'YES' with 'YES' and replace
I want to replace the class with the div text like this : This:

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.