简体   繁体   中英

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM