python - 使用Python的regex .match()方法获取下划线前后的字符串

标签 python regex string iterator

我有以下代码:

tablesInDataset = ["henry_jones_12345678", "henry_jones", "henry_jones_123"]

for table in tablesInDataset:
    tableregex = re.compile("\d{8}")
    tablespec = re.match(tableregex, table)

    everythingbeforedigits = tablespec.group(0)
    digits = tablespec.group(1)

我的正则表达式应该只返回下划线后包含 8 位数字的字符串。一旦它返回字符串,我想使用 .match() 使用 .group() 方法获取两个组。第一组应包含一个字符串,其中包含数字之前的所有字符,第二组应包含一个包含 8 位数字的字符串。

使用 .match().group() 获得我正在寻找的结果的正确正则表达式是什么?

最佳答案

使用捕获组:

>>> import re
>>> pat = re.compile(r'(?P<name>.*)_(?P<number>\d{8})')
>>> pat.findall(s)
[('henry_jones', '12345678')]

如果需要,您可以获得命名组的好特性:

>>> match = pat.match(s)
>>> match.groupdict()
{'name': 'henry_jones', 'number': '12345678'}

关于python - 使用Python的regex .match()方法获取下划线前后的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39153351/

相关文章:

java - 如何获取当前选项卡的标题

python - 使用Python将日记文件拆分为多个文件

python - 在 pylearn2 中使用 RBM 预训练 ANN

python - python 中对象的字典键项

Python 正则表达式在>之后使用正向lookbehind进行匹配

regex - 正则表达式允许 1-3 位数字之间的数值吗?

python - 组合和排序元组列表的最快方法是什么?

javascript - 标记化字符串的正则表达式

string - 按字符分割字符串

string - 子串的相对位置