Shery
Shery

Reputation: 1882

How to replace 2 or more special characters or underscores appearing together in a string Python

I have the following string:

'___5weeks_rollingAverage_8hours'
'___5weeks__rolling=*%Average_8hours'
'___5weeks_rollingAverage_8hours__'
'___5weeks_rollingAverage_8hours'

I wish to remove _ if it appears more than once anywhere in the string. So the above should translate to:

5weeks_rollingAverage_8hours

tvm

Upvotes: 0

Views: 283

Answers (2)

Siyanew
Siyanew

Reputation: 602

you can use regular expression as you said for non alphanumeric we can use [^a-zA-Z0-9] so the code is something like this:

import re
regex = re.compile(r'[^a-zA-Z0-9]{2,}')
t = '___5weeks_rollingAverage___8h**ou&%rs'

result = regex.sub("",t)
print(result)

Output: 5weeks_rollingAverage8hours

Upvotes: 1

buran
buran

Reputation: 14233

using regex

import re

test_str = ("___5weeks_rollingAverage_8hours", "___5weeks__rolling=*%Average_8hours",
            "___5weeks_rollingAverage_8hours__", "___5weeks_rollingAverage_8hours")

regex = re.compile(r"[!\"#$%&'()*+,-.\/:;<=>?@\[\\\]^_`{|}~]{2,}")
for item in test_str:
    item = regex.sub('', item)
    print(item)

output

5weeks_rollingAverage_8hours
5weeksrollingAverage_8hours
5weeks_rollingAverage_8hours
5weeks_rollingAverage_8hours

Upvotes: 0

Related Questions