Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a string matches a set pattern in Python?

I want to match a string to a specific pattern or set of words, like below:

the apple is red is the query and the apple|orange|grape is red|orange|violet is the pattern to match. The pipes would represent words that would substitute each other. The pattern could also be grouped like [launch app]|[start program]. I would like the module to return True or False whether the query matches the pattern, naturally.

What is the best way to accomplish this if there is not a library that does this already? If this can be done with simple regex, great; however I know next to nothing about regex. I am using Python 2.7.11

like image 935
Galen Nare Avatar asked Jan 29 '23 22:01

Galen Nare


2 Answers

import re

string = 'the apple is red'

re.search(r'^the (apple|orange|grape) is (red|orange|violet)', string)

Here's an example of it running:

In [20]: re.search(r'^the (apple|orange|grape) is (red|orange|violet)', string). groups()
Out[20]: ('apple', 'red')

If there are no matches then re.search() will return nothing.

You may know "next to nothing about regex" but you nearly wrote the pattern.

The sections within the parentheses can also have their own regex patterns, too. So you could match "apple" and "apples" with

r'the (apple[s]*|orange|grape)

like image 52
Cory Madden Avatar answered Feb 02 '23 10:02

Cory Madden


The re based solutions for this kind of problem work great. But it would sure be nice if there were an easy way to pull data out of strings in Python without have to learn regex (or to learn it AGAIN, which what I always end up having to do since my brain is broken).

Thankfully, someone took the time to write parse.

parse

parse is a nice package for this kind of thing. It uses regular expressions under the hood, but the API is based on the string format specification mini-language, which most Python users will already be familiar with.

For a format spec you will use over and over again, you'd use parse.compile. Here is an example:

>>> import parse
>>> theaisb_parser = parse.compile('the {} is {}')
>>> fruit, color = theaisb_parser.parse('the apple is red')
>>> print(fruit, color)
apple red
like image 26
Rick supports Monica Avatar answered Feb 02 '23 11:02

Rick supports Monica