正则表达式删除python中数字前的逗号 [英] Regex to remove commas before a number in python

查看:99
本文介绍了正则表达式删除python中数字前的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用逗号作为分隔符来处理文件.但是,它具有一个字段,地址在其中,地址的格式为x,y,z,这会导致问题,因为该地址的每个部分都获得了新的列条目.该地址后面紧跟member_no一个1位数的数字,例如2等.Col1(地址),Col2(1位数字)

I'm working with a file using commas as a delimiter. However, it has a field, address in it where the address is of form x,y,z which causes a problem as each part of the address gets a new column entry. The address is immediately followed by member_no a 1 digit number like 2 etc. Col1 (Address), Col2(1 Digit number)

text = '52A, XYZ Street, ABC District, 2'

我基本上想从地址字段中删除该数字之前的所有逗号.

I basically want to remove all commas before that number from the address field.

输出应该像

52A XYZ Street ABC District, 2'

我尝试了

re.sub(r',', ' ', text)

但是它将替换所有逗号.

but it's replacing all instances of commas.

推荐答案

使用零宽度的负前瞻以确保要替换的子字符串(此处为逗号)后没有 {space}(s){数字} 结尾:

Use a zero-width negative lookahead to make sure the to be replaced substrings (commas here) are not followed by {space(s)}{digit} at the end:

,(?!\s+\d$)

示例:

In [227]: text = '52A, XYZ Street, ABC District, 2'

In [228]: re.sub(',(?!\s+\d$)', '', text)
Out[228]: '52A XYZ Street ABC District, 2'


如果您在,{space}(数字)子字符串后有更多逗号,并希望保留所有逗号,请在后面加上负数以确保逗号前面没有 {space} {digit< or> [AZ]} :

If you have more commas after the ,{space(s)}{digit} substring, and want to keep them all, leverage a negative lookbehind to make sure the commas are not preceded by {space}{digit<or>[A-Z]}:

(?<!\s[\dA-Z]),(?!\s+\d,?)

示例:

In [229]: text = '52A, XYZ Street, ABC District, 2, M, Brown'

In [230]: re.sub('(?<!\s[\dA-Z]),(?!\s+\d,?)', '', text)
Out[230]: '52A XYZ Street ABC District, 2, M, Brown'

In [231]: text = '52A, XYZ Street, ABC District, 2'

In [232]: re.sub('(?<!\s[\dA-Z]),(?!\s+\d,?)', '', text)
Out[232]: '52A XYZ Street ABC District, 2'

这篇关于正则表达式删除python中数字前的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆