0

I'm using the solution obtained from this question Regular expression to match any character being repeated more than 10 times

The regex you need is /(.)\1{9,}/.

https://regex101.com/ is recognizing it, grep recognizes it, but python does not.

Ultimately I want to replace the match with a single space, for example:

>> text = 'this is text???????????????'
>> pattern = re.compile(r'/(.)\1{5,}/')
>> re.sub(pattern,'\s',text)
'this is text '

However, search, findall, even match do not recognize the pattern, any idea as to why?

1
  • 1
    Pythex.org is your friend Commented Apr 6, 2016 at 0:27

1 Answer 1

3
re.sub(r'(.)\1{9,}', ' ',text)

The slashes are not part of the regex, they are a syntactic construct by which some languages form regex literals (and in case of PHP's preg module, an oddity).

With your regexp, you would have matched this is text/?????????????/, and transformed it into this is text\s (note that \s has no meaning in the replacement string).

Sign up to request clarification or add additional context in comments.

3 Comments

The hilarious part is that the second answer to that question contains the correct regex for Python.
@IgnacioVazquez-Abrams: Oh haha indeed!
Even tho it was included, the explanation was great. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.