re.match matches only at the beginning of the string.
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.
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 tags before going any further.
Answer from mouad on Stack Overflowpython - how to test for a regex match - Stack Overflow
rexi: A Terminal UI for Regex Testing Built in Python
Best site to learn regex?
I don't understand regex at all
Videos
Hey everyone,
I'm excited to share a project I've been working on called rexi. It's a CLI tool designed to make regex testing more interactive and accessible directly from your terminal. Built with Python and utilizing the textual library, rexi offers a sleek terminal UI where you can effortlessly test your regex patterns.
Key Features:
Interactive terminal UI for an enhanced user experience.
Supports
matchandfinditermodes for regex evaluation.Real-time feedback on regex patterns with marked outputs.
Why rexi?
I created rexi to streamline the process of testing regex patterns. It's perfect for those who prefer working within the terminal or need a quick way to validate and learn regex through immediate feedback.
Getting Started:
Getting started with rexi is simple. After installation, just pipe your input text into rexi and start testing your regex patterns in an interactive UI. Here's a quick peek at how you can use it:
echo "your sample text" | rexi
Check it out and let me know what you think! I'm open to any suggestions, bug reports, or contributions to make rexi even better.
The repo: https://github.com/royreznik/rexi