-3

I want to replace the beginning two letters "or" in "organization" with "*" useing python's re.sub function. I read some books about regular expression that say \b represents word boundary, so I used \bor as the pattern. But I got:

>>> re.sub("\bor","*","organization")
'organization'
>>>

I think I should got *ganization. What's the matter and how should I use re pattern to get what I want? Thank you.

Stephan
  • 41,764
  • 65
  • 238
  • 329
user2384994
  • 1,759
  • 4
  • 24
  • 29

2 Answers2

2

You need to use a raw-string here:

>>> import re
>>> re.sub(r"\bor", "*", "organization")
'*ganization'
>>>

Otherwise, Python sees \b, which gets translated to \x08:

>>> '\b'
'\x08'
>>>

Another solution would be to escape the backslash:

>>> import re
>>> re.sub("\\bor", "*", "organization")
'*ganization'
>>>

Yet another (and probably the best) solution would be to use ^ in place of \b:

>>> import re
>>> re.sub("^or", "*", "organization")
'*ganization'
>>>

In Regex, using ^ like this means "match at the start of the string".

0

Here is another way that you could do this using the built-in replace function.

string = "organization"
print string.replace(string[:2], "*")
kyle k
  • 5,134
  • 10
  • 31
  • 45