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
This should work:: \W{3,}matching non-alphanumeric characters that occur 3 or more times:
\W{3,}
>>> s = "***(((((hi abc ***&&&&" >>> re.sub("\W{3,}", "", s) 'hi abc' >>> s = "(((((hi abc )))))))" >>> re.sub("\W{3,}", "", s) 'hi abc'
- -- (, '!?&', ), @Stephen . , :
'!?&'
>>> r3 = re.compile(r'(([^\s\w])\2{2,})') >>> r3.findall('&&&xxx!&?yyy*****') [('&&&', '&'), ('*****', '*')]
, :
>>> r3.sub('', '&&&xxx!&?yyy*****') 'xxx!&?yyy'
(, ) " " , . "" " " ; "*" , , .
""
" "
"*"
>>> re.sub(r"(\W)\1{3,}", "*", "12345<><>aaaaa%%%11111<<<<..>>>>") '12345<><>aaaaa%%%11111*..*' >>>
: "< > < > "... , " , 3 " ". , " 3 ", " 3 ".
Source: https://habr.com/ru/post/1756092/More articles:Transaction Type Security - designClass inheritance and polymorphism - drawing simple forms - polymorphismCSS only dropdown menu - htmlWhere to apply: hover over to save css submenu in hover state - htmlManaging multiple azure bursts - azureWCF does not bind to TCP port (check via netstat -a) - vb.netAn exception to the animation block - iphonePass values ββfrom an array to an object? - PHP - arraysMerge-based Ruby on Rails Route - ruby-on-railsWhat are the best practices for working with oEmbed regarding storing and caching responses? - htmlAll Articles