1

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

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

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

m.group(0)返回

',1620693,'
4

2 回答 2

7

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'
于 2013-02-12T01:58:06.193 回答
3

使用m.group(1). 您也不需要转义(反斜杠)逗号。 m.group(0)指的是整个比赛,后面的每个数字指的是匹配的组。

于 2013-02-12T01:57:58.953 回答