[英]Extract text from Regular Expression?
I'm trying to get the results of some matched text in a regular expression, but it doesn't seem to work. 我试图在正则表达式中获得一些匹配文本的结果,但它似乎不起作用。 Anyone know what might be going wrong?
有谁知道可能会出错?
import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>[^\>]+)>", text)
if match:
print (match.group('brackets'))
This prints nothing, ie no match found. 这没有打印,即找不到匹配。
This is actually a really common error -- it looks like you're using re.match
, when you wanted to use re.search
. 这实际上是一个非常常见的错误 - 当你想使用
re.search
时,看起来你正在使用re.match
。 re.match
only matches from the beginning of the given text, whereas re.search
checks the entire thing. re.match
只匹配给定文本的开头,而re.search
检查整个事物。
import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>[^\>]+)>", text)
if match:
print (match.group('brackets'))
Output: 输出:
'angle brackets'
While @Tom Jacques has answered the question very nicely, the code shown in both the question and answer didn't work for me when I tried it. 虽然@Tom Jacques非常好地回答了这个问题,但当我尝试时,问题和答案中显示的代码对我来说都不起作用。 The following code worked:
以下代码有效:
import re
text = "I want to match anything in <angle brackets>"
match = re.search("\<(?P<brackets>.*)\>",text)
if match:
print (match.group('brackets'))
Note the replacement of the text [^
with .*)
in the regular expression and the inclusion of the text
parameter in the call to re.search()
. 注意在正则表达式中替换文本
[^
with .*)
,并在调用re.search()
包含text
参数。
(EDIT) (编辑)
This answer addresses an issue that has since been corrected in both the question and the other answer. 这个答案解决了一个问题,该问题已在问题和其他答案中得到纠正。 The change to the regular expression proposed here would capture all text up to the last
>
on the line, whereas the changed regular expression in the question and the other answer would capture text only up to the first >
that it finds. 对此处提出的正则表达式的更改将捕获到行中最后一个
>
所有文本,而问题和另一个答案中更改的正则表达式将捕获文本,直到它找到的第一个>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.