Skip to main content
 首页 » 编程设计

python之正则表达式:如何在我的模式中排除逗号

2025年05月04日23zhenyulu

我想找到两端夹有逗号的第一个数字,我想出了这个:

m = re.search("\,([0-9])*\,",line) 

但是,这会返回带逗号的数字,我该如何排除它们?

m.group(0) 返回

',1620693,' 

请您参考如下方法:

group(0) 将始终返回整个匹配项。

参见 python 文档:

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") 
>>> m.group(0)       # The entire match 
'Isaac Newton' 
>>> m.group(1)       # The first parenthesized subgroup. 
'Isaac'