简体   繁体   English

从正则表达式中提取文本?

[英]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.

相关问题 使用正则表达式从文本中提取类别 - Extract categories from text using regular expression 正则表达式从文本文件中提取文本块? - Regular expression to extract chunks of text from a text file? 如何使用正则表达式从一行文本中提取数值? - How to extract a numeric value from a line of text with a regular expression? 从给定文本中提取软件版本的正则表达式? - Regular expression to extract software version from the given text? 如何基于正则表达式模式从文本文件中提取数据 - How to extract data from a text file based on a regular expression pattern python-正则表达式从文件中提取某些文本数据 - python - Regular expression to extract certain text data from a file 正则表达式从 Python 中的文本中提取带有尺寸的数量 - Regular Expression to extract quantity with dimensions from text in Python 从 dataframe 中的一列中提取和拆分文本的正则表达式 - Regular expression to extract and split text from one column in dataframe 正则表达式仅基于大小写从文本中提取命名实体 - Regular Expression to extract Named Entities from text just based on capitalization 是否有任何正则表达式用于从文本中查找和提取字符串 - Is there any regular expression for finding and extract the string from a text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM