获取 re.search python 的匹配值
Getting the match value of re.search python
我正在使用 Jupyter 笔记本中的 python 从网络中提取一些数据。我已经下载了数据、解析并创建了数据框。我需要从数据框中的字符串中提取一个数字。我利用这个正则表达式来做到这一点:
for note in df["person_notes"]:
print(re.search(r'\d+', note))
结果如下:
<_sre.SRE_Match object; span=(53, 55), match='89'>
我怎样才能得到比赛号码;在这一行中将是 89。我试图将整行转换为 str()
和 replace()
,但并非所有行都具有 span=(number, number)
相等。提前致谢!
您可以对返回的匹配对象使用 start()
和 end()
方法来获取字符串中的正确位置:
for note in df["person_notes"]:
match = re.search(r'\d+', note)
if match:
print(note[match.start():match.end()])
else:
# no match found ...
我正在使用 Jupyter 笔记本中的 python 从网络中提取一些数据。我已经下载了数据、解析并创建了数据框。我需要从数据框中的字符串中提取一个数字。我利用这个正则表达式来做到这一点:
for note in df["person_notes"]:
print(re.search(r'\d+', note))
结果如下:
<_sre.SRE_Match object; span=(53, 55), match='89'>
我怎样才能得到比赛号码;在这一行中将是 89。我试图将整行转换为 str()
和 replace()
,但并非所有行都具有 span=(number, number)
相等。提前致谢!
您可以对返回的匹配对象使用 start()
和 end()
方法来获取字符串中的正确位置:
for note in df["person_notes"]:
match = re.search(r'\d+', note)
if match:
print(note[match.start():match.end()])
else:
# no match found ...