Reputation: 1274
I'm tring to find <20> UNIQUE Registered
in hostname <20> UNIQUE Registered
and replace it with ""
in python. Below is my code. Please inform me where my syntax is wrong to replace this string:
string = string.replace(r'<\d*2> UNIQUE Registered ', "")
Upvotes: 1
Views: 48
Reputation: 473813
replace()
cannot do regular expression substitution. Use re.sub()
instead:
>>> import re
>>> s = "hostname <20> UNIQUE Registered"
>>> re.sub(r"<\d{2}>\s+UNIQUE\s+Registered", "", s)
'hostname '
where \d{2}
would match 2 subsequent digits, \s+
- one or more space characters.
As a side note, could not you just split the string by space and get the first item:
>>> s.split()[0]
'hostname'
Upvotes: 4