Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Your Python Regex Pattern Doesn’t Match? Try This!
1
Your Python Regex Pattern Doesn’t Match? Try This!

5/5 – (1 vote)

Problem Formulation

Say, you want to find a regex pattern in a given string. You know the pattern exists in the string. You use the
Code:
re.match(pattern, string)
function to find the match object where the pattern matches in the string.

[Image: 1f4ac.png] Problem: The Python regular expression pattern is not found in the string. The pattern doesn’t match anything, and, thus, the match object is
Code:
None
. How to fix this?

Here’s an example in which you’re searching for the pattern
Code:
'h[a-z]+'
which should match the substring
Code:
'hello'
.

But it doesn’t match! [Image: 26a1.png]

Code:
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.match(pattern, my_string) if match: print('found!') else: print('not found!')

Output:

Code:
not found!

Where is the bug? And how to fix it, so that the pattern matches the substring
Code:
'hello'
?

[Image: 1f4a1.png] Learn More: Improve your regex superpower by studying character classes used in the example pattern
Code:
'h[a-z]+'
by visiting this tutorial on the Finxter blog.

Solution: Use re.search() instead of re.match()

A common reason why your Python regular expression pattern is not matching in a given string is that you mistakenly used
Code:
re.match(pattern, string)
instead of
Code:
re.search(pattern, string)
or
Code:
re.findall(pattern, string)
. The former attempts to match the
Code:
pattern
at the beginning of the
Code:
string
, whereas the latter two functions attempt to match anywhere in the string.

Here’s a quick recap of the three regex functions:
  • Code:
    re.match(pattern, string)
    returns a match object if the
    Code:
    pattern
    matches at the beginning of the
    Code:
    string
    . The match object contains useful information such as the matching groups and the matching positions.

  • Code:
    re.search(pattern, string)
    matches the first occurrence of the
    Code:
    pattern
    in the
    Code:
    string
    and returns a matchobject.

  • Code:
    re.findall(pattern, string)
    scans
    Code:
    string
    from left to right, searching for all non-overlapping matches of the
    Code:
    pattern
    . It returns a list of strings in the matching order when scanning the string from left to right.

Thus, the following code uses re.search() to fix our problem:

Code:
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.search(pattern, my_string) if match: print('found!') else: print('not found!')

Output:

Code:
found!

Finally, the pattern
Code:
'h[a-z]+'
does match the string
Code:
'hello world'
.

Note that you can also use the re.findall() function if you’re interested in just the string matches of your pattern (without match object). We’ll explain all of this — re.match(), re.search(), re.findall(), and match objects — in a moment but first, let’s have a look at the same example with re.findall():

Code:
import re my_string = 'hello world' pattern = re.compile('h[a-z]+') match = re.findall(pattern, my_string) print(match) # ['hello'] if match: print('found!') else: print('not found!')

Output:

Code:
['hello'] found!
Understanding re.match()



The
Code:
re.match(pattern, string)
method returns a match object if the
Code:
pattern
matches at the beginning of the
Code:
string
. The match object contains useful information such as the matching groups and the matching positions. An optional argument
Code:
flags
allows you to customize the regex engine, for example to ignore capitalization.

Specification:

Code:
re.match(pattern, string, flags=0)

The
Code:
re.match()
method has up to three arguments.
  • Code:
    pattern
    : the regular expression pattern that you want to match.

  • Code:
    string
    : the string which you want to search for the pattern.

  • Code:
    flags
    (optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.

We’ll explore them in more detail later.

Return Value:

The
Code:
re.match()
method returns a match object. You may ask (and rightly so):

[Image: 1f4a1.png] Learn More: Understanding
Code:
re.match()
on the Finxter blog.

What’s a Match Object?

If a regular expression matches a part of your string, there’s a lot of useful information that comes with it: what’s the exact position of the match? Which regex groups were matched—and where?

The match object is a simple wrapper for this information. Some regex methods of the re package in Python—such as
Code:
search()
—automatically create a match object upon the first pattern match.

At this point, you don’t need to explore the match object in detail. Just know that we can access the start and end positions of the match in the string by calling the methods
Code:
m.start()
and
Code:
m.end()
on the match object
Code:
m
:

Code:
>>> m = re.search('h...o', 'hello world') >>> m.start() 0 >>> m.end() 5 >>> 'hello world'[m.start():m.end()] 'hello'

In the first line, you create a match object m by using the
Code:
re.search()
method. The pattern
Code:
'h...o'
matches in the string
Code:
'hello world'
at start position 0.

You use the start and end position to access the substring that matches the pattern (using the popular Python technique of slicing).

Now that you understood the purpose of the match object, let’s have a look at the alternative to the
Code:
re.match()
function next! [Image: 1f680.png]

Understanding re.search()



The
Code:
re.search(pattern, string)
method matches the first occurrence of the
Code:
pattern
in the
Code:
string
and returns a matchobject.

Specification:

Code:
re.search(pattern, string, flags=0)

The
Code:
re.search()
method has up to three arguments.
  • Code:
    pattern
    : the regular expression pattern that you want to match.

  • Code:
    string
    : the string which you want to search for the pattern.

  • Code:
    flags
    (optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.

We’ll explore them in more detail later.

Return Value:

The
Code:
re.search()
method returns a match object. You may ask (and rightly so):

[Image: 1f4a1.png] Learn More: Understanding
Code:
re.search()
on the Finxter blog.

Understanding re.findall()



The
Code:
re.findall(pattern, string)
method scans
Code:
string
from left to right, searching for all non-overlapping matches of the
Code:
pattern
. It returns a list of strings in the matching order when scanning the string from left to right.

[Image: refindall-1024x576.jpg]

Specification:

Code:
re.findall(pattern, string, flags=0)

The
Code:
re.findall()
method has up to three arguments.
  • Code:
    pattern
    : the regular expression pattern that you want to match.

  • Code:
    string
    : the string which you want to search for the pattern.

  • Code:
    flags
    (optional argument): a more advanced modifier that allows you to customize the behavior of the function. Want to know how to use those flags? Check out this detailed article on the Finxter blog.

We will have a look at each of them in more detail.

Return Value:

The
Code:
re.findall()
method returns a list of strings. Each string element is a matching substring of the string argument.

[Image: 1f4a1.png] Learn More: Understanding
Code:
re.findall()
on the Finxter blog.

Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.  

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages. 

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.  Regular expressions ​rule the game ​when text processing ​meets computer science. 

If you want to become a regular expression master too, check out themost comprehensive Python regex course on the planet:

[Image: ClickToPlay-1024x576.jpg]

Now, this was a lot of theory! Let’s get some practice.

In my Python freelancer bootcamp, I’ll train you on how to create yourself a new success skill as a Python freelancer with the potential of earning six figures online.

The next recession is coming for sure, and you want to be able to create your own economy so that you can take care of your loved ones.

Check out my free “Python Freelancer” webinar now!

Join 20,000+ ambitious coders for free!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: