简体   繁体   English

如何使用 Python 正则表达式精确匹配提取 substring

[英]How to extract substring with Python Regex Exact Match

I'm learning Python Regular Expression (re) to analyze twitter text.我正在学习 Python 正则表达式(重新)来分析 twitter 文本。

Let's say I have twitter text like below and I only want to extract exactly '3/10' from txt.假设我有如下 twitter 文本,我只想从 txt 中准确提取“3/10”。
Python return empty list [] in this case. Python 在这种情况下返回空列表 []。

txt = "my mood is low 3/10. 05/01/2021 Tuesday"
re.findall('^\d+\/\d{2}$', txt)

What's wrong with my code?我的代码有什么问题?

Instead of using anchors to match the whole line, you can use negative lookarounds to assert a whitespace boundary to the left, and not a / to the right to match 3/10 only.除了使用锚来匹配整行,您可以使用否定环视来断言左侧的空白边界,而不是右侧的/以仅匹配3/10

(?<!\S)\d+\/\d{2}(?!/)

Regex demo正则表达式演示

import re
txt = "my mood is low 3/10. 05/01/2021 Tuesday"
print(re.findall('(?<!\S)\d+\/\d{2}(?!/)', txt))

Output Output

['3/10']

Remove the ^ and $删除^$

re.findall(r'\b\d+/\d{2}\b', txt)

According to re docs根据重新文档

^ (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline. ^ (Caret.) 匹配字符串的开头,并且在 MULTILINE 模式下也会在每个换行符之后立即匹配。

$ Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. $匹配字符串的结尾或字符串结尾的换行符之前,并且在 MULTILINE 模式下也匹配换行符之前。 foo matches both 'foo' and 'foobar', while the regular expression foo$ matches only 'foo'. foo 匹配 'foo' 和 'foobar',而正则表达式 foo$ 只匹配 'foo'。 More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches 'foo2' normally, but 'foo1' in MULTILINE mode;更有趣的是,在 'foo1\nfoo2\n' 中搜索 foo.$ 通常匹配 'foo2',但在 MULTILINE 模式下搜索 'foo1'; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string.在 'foo\n' 中搜索单个 $ 将找到两个(空)匹配项:一个在换行符之前,一个在字符串末尾。

This is not case in your example.在您的示例中不是这种情况。 You would need to use more advanced zero-length assertions.您将需要使用更高级的零长度断言。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM