1

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 ', "")

1 Answer 1

4

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'
Sign up to request clarification or add additional context in comments.

1 Comment

The split method worked. I couldn't seem to get the sub method to work

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.