Information Technology
Information Technology

Reputation: 2333

Regular expression in python to substitute commas bounded by whole numbers with periods

I have a string where there is an errant comma (',') in an IP address that should just be a period ('.'). The whole string is:

a = 'This is a test, which uses commas for a bad IP Address. 54.128,5,5, 4.'

In the above string, the IP address 54.128,5,5 should be 54.128.5.5

I tried to use re.sub(), as follows, but it doesn't seem to work...

def stripBadCommas(string):
  newString = re.sub(r'/(?<=[0-9]),(?<=[0-9])/i', '.', string)
  return newString

a = 'This is a test, which uses commas for a bad IP Address. 54.128,5,5, 4.'
b = ''
b = stripBadCommas(a)
print a
print b

MY QUESTION: What is the proper way to use Regular Expressions to search for and replace only the commas that are bounded by whole numbers/numerics with periods without disrupting the other appropriate commas and periods?

Thanks, in advance, for any assistance you can offer.

Upvotes: 4

Views: 49

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

You may use

def stripBadCommas(s):
  newString = re.sub(r'(?<=[0-9]),(?=[0-9])', '.', s)
  return newString

See the Python online demo.

Note that Python re patterns are not written using regex literal notations, the / and /i are treated as part of the pattern. Moreover, the pattern needs no case insensitive modifier as it has no letters inside (does not match case letters).

Besides, you used the second lookbehind (?<=[0-9]) while there must be a positive lookahead (?=[0-9]) because ,(?<=[0-9]) pattern never matches (the , is matched and then the engine tries to make sure the , is a digit, which is false).

Upvotes: 2

Related Questions