BLang
BLang

Reputation: 990

python regular expression not evaluating with \b

I am trying to do a simple boundary regular expressions substitution on strings with dashes, but they do not replace, why is that, do the dashes mean anything in regex?

Here is my actual attempt where f is the variable where i want to replace the string exact matches:

>>> import re
>>> f = "user-services-http-two user-services-http"
>>> re.sub(r'\buser-services-http\b', '32370', f)
'32370-two 32370'

How would i get the desired output of:

'user-services-http-two 32370'

My requirement is that i have to use re since i am using another module in python (massedit) later on in my code.

Upvotes: 2

Views: 64

Answers (2)

Aurielle Perlmann
Aurielle Perlmann

Reputation: 5509

import re

f = "user-services-http-two user-services-http"
re.sub('\\b(user-services-http$)', '32370', f)

edited based on your extra comment above - but its the same as PKumar - it works for all your cases

Upvotes: 1

PKumar
PKumar

Reputation: 11128

You can do this:

re.sub(r'\buser-services-http$', '32370', f)

Upvotes: 1

Related Questions