简体   繁体   English

正则表达式:在模式中如何排除逗号?

[英]Regex: How do I exclude commas when in my pattern?

I want to find the first number that is sandwiched with commas on either end, and I came up with this: 我想找到第一个数字,两端都用逗号隔开,然后我想到了:

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

However, this returns to me the number with the commas, how do I exclude them? 但是,这给我返回带逗号的数字,如何排除它们?

m.group(0) returns m.group(0)返回

',1620693,'

group(0) will always return the entire match. group(0)将始终返回整个匹配项。

See python documentation: 请参阅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'

Use m.group(1) . 使用m.group(1) You also don't need to escape (backslash) the commas. 您也不需要转义(反斜杠)逗号。 m.group(0) refers to the entire match, and each number after that refers to matched groups. m.group(0)表示整个匹配项,其后的每个数字表示匹配的组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 RegEx 模式传递给 Pytesseract? - How do I pass a RegEx pattern to Pytesseract? 如何从列表输出中删除逗号和引号 - How do I remove commas and quotations from my list output 正则表达式排除数字模式 - Regex to exclude number pattern 正则表达式排除特定模式 - Regex exclude a specific pattern 正则表达式或不起作用 - 我不知道我的模式有什么问题 - regex or does not work - I do not know what is wrong in my pattern 在 Python 中捕获正则表达式组时,如何排除字符? - How can I exclude a character when capturing regex group in Python? 如何提取由&lt;&gt;界定并以逗号分隔的引号引起的元素列表-python,regex? - How do i extract a list of elements encased in quotation marks bounded by <> and delimited by commas - python, regex? 如何为我的python正则表达式执行“或”操作? - How do I do an “OR” for my python regex? 在导入带有额外逗号的熊猫的csv文件时,如何使用正则表达式作为分隔符? - How can I use regex as a delimiter when importing a csv file with pandas with extra commas? RegEx排除目录,捕获用逗号分隔的文件名,排除“(number)”和扩展名 - RegEx to exclude directory, capture filename that are separated by commas, exclude “(number)” and extensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM