Python 如何测试正则表达式匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4742662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:18:13  来源:igfitidea点击:

how to test for a regex match

pythonregex

提问by jml

I have a string. Let's call it 'test'. I want to test a match for this string, but only using the backref of a regex.

我有一个字符串。我们称之为“测试”。我想测试这个字符串的匹配,但只使用正则表达式的 backref。

Can I do something like this:

我可以做这样的事情:

import re

进口重新

for line in f.readlines():
   if '<a href' in line:
      if re.match('<a href="(.*)">', line) == 'test':
         print 'matched!'

? This of course, doesn't seem to work, but I would think that I might be close? Basically the question is how can I get re to return only the backref for comparison?

? 这当然似乎行不通,但我认为我可能会接近?基本上问题是我怎样才能让 re 只返回 backref 进行比较?

采纳答案by mouad

re.matchmatches only at the beginningof the string.

re.match只匹配字符串的开头

def url_match(line, url):
    match = re.match(r'<a href="(?P<url>[^"]*?)"', line)
    return match and match.groupdict()['url'] == url:

example usage:

用法示例:

>>> url_match('<a href="test">', 'test')
True
>>> url_match('<a href="test">', 'te')
False
>>> url_match('this is a <a href="test">', 'test')
False

If the pattern could occur anywhere in the line, use re.search.

如果该模式可能出现在行中的任何位置,请使用re.search.

def url_search(line, url):
    match = re.search(r'<a href="(?P<url>[^"]*?)"', line)
    return match and match.groupdict()['url'] == url:

example usage:

用法示例:

>>> url_search('<a href="test">', 'test')
True
>>> url_search('<a href="test">', 'te')
False
>>> url_search('this is a <a href="test">', 'test')
True

N.B : If you are trying to parsing HTML using a regex, read RegEx match open tags except XHTML self-contained tagsbefore going any further.

注意:如果您尝试使用正则表达式解析 HTML,请在继续之前阅读RegEx 匹配打开的标签(XHTML 自包含标签除外)