How to do this, replace regex in python?

Given a line of text in Python:

s = "(((((hi abc )))))))"
s = "***(((((hi abc ***&&&&"

How to replace all non-alphabetic characters that occur more than three times ... as an empty string

For all of the above, the result should be:

hi abc
+3
source share
3 answers

This should work:: \W{3,}matching non-alphanumeric characters that occur 3 or more times:

>>> s = "***(((((hi abc ***&&&&"
>>> re.sub("\W{3,}", "", s) 
'hi abc'
>>> s = "(((((hi abc )))))))"
>>> re.sub("\W{3,}", "", s) 
'hi abc'
+8
source

- -- (, '!?&', ), @Stephen . , :

>>> r3 = re.compile(r'(([^\s\w])\2{2,})')
>>> r3.findall('&&&xxx!&?yyy*****')
[('&&&', '&'), ('*****', '*')]

, :

>>> r3.sub('', '&&&xxx!&?yyy*****')
'xxx!&?yyy'
+4

(, ) " " , . "" " " ; "*" , , .

>>> re.sub(r"(\W)\1{3,}", "*", "12345<><>aaaaa%%%11111<<<<..>>>>")
'12345<><>aaaaa%%%11111*..*'
>>>

: "< > < > "... , " , 3 " ". , " 3 ", " 3 ".

0
source

Source: https://habr.com/ru/post/1756092/


All Articles