Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python One Line Regex Match
1
Python One Line Regex Match

Summary: To match a
Code:
pattern
in a given
Code:
text
using only a single line of Python code, use the one-liner
Code:
import re; print(re.findall(pattern, text))
that imports the regular expression library re and prints the result of the
Code:
findall()
function to the shell.

Problem: Given a string and a regular expression pattern. Match the string for the regex pattern—in a single line of Python code!

Example: Consider the following example that matches the pattern
Code:
'F.*r'
against the string
Code:
'Learn Python with Finxter'
.

Code:
import re s = 'Learn Python with Finxter' p = 'F.*r' # Found Match of p in s: 'Finxter'

Let’s dive into the different ways of writing this into a single line of Python code!

https://repl.it/@finxter/BriskAcidicWatch?lite=true

Exercise: Run the code. What’s the output of each method? Why does the output differ?

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Method 1: findall()

The re.findall(pattern, string, flags=0) method returns a list of string matches. Read more in our blog tutorial.

Code:
# Method 1: findall() import re; print(re.findall('F.*r', 'Learn Python with Finxter')) # ['Finxter']

There’s no better way of importing the
Code:
re
library and calling the
Code:
re.findall()
function in a single line of code—you must use the semicolon
Code:
A;B
to separate the statements
Code:
A
and
Code:
B
.

The
Code:
findall()
function finds all occurrences of the pattern in the string.

Method 2: search()

The re.search(pattern, string, flags=0) method returns a match object of the first match. Read more in our blog tutorial.

Code:
# Method 2: search() import re; print(re.search('F.*r', 'Learn Python with Finxter')) # <re.Match object; span=(18, 25), match='Finxter'>

The
Code:
search()
function finds the first match of the pattern in the string and returns a matching object

Method 3: match()

The re.match(pattern, string, flags=0) method returns a match object if the regex matches at the beginning of the string. Read more in our blog tutorial.

Code:
# Method 3: match() import re; print(re.match('.*F.*r', 'Learn Python with Finxter')) # <re.Match object; span=(0, 25), match='Learn Python with Finxter'>

The
Code:
match()
function finds the match of the pattern at the beginning of the string and returns a matching object. In this case, the whole string matches, so the match object encloses the whole string.

Method 4: fullmatch()

The re.fullmatch(pattern, string, flags=0) method returns a match object if the regex matches the whole string. Read more in our blog tutorial.

Code:
# Method 4: fullmatch() import re; print(re.fullmatch('.*F.*r.*', 'Learn Python with Finxter')) #<re.Match object; span=(0, 25), match='Learn Python with Finxter'>

The
Code:
fullmatch()
function attempts to match the whole string and returns a matching object if successful. In this case, the whole string matches, so the match object encloses the whole string.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: