5

Python Regular Expressions Tutorial

 3 years ago
source link: https://www.pythoncentral.io/series/python-regular-expressions-tutorial/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A series on using regular expressions with Python, with examples. Including matching characters, repetitions, captured groups, assertions, and functions.

Introduction to Python Regular Expressions

Searching within a string for another string is pretty easy in Python:

>>> str = 'Hello, world'
>>> print(str.find('wor'))

This is fine if we know exactly what we’re looking for, but what if we’re looking for something not so well-defined? For example, if we want to search for a year, then we know it’s going to be a 4-digit sequence, but […]

Read More

More About Python Regular Expressions

In the first part of this series, we looked at the basic syntax of regular expressions and some simple examples. In this part, we’ll take a look at some more advanced syntax and a few of the other features Python has to offer. Regular Expression Captured Groups So far, we’ve searched within a string using […]

Read More

The Odds & Ends of Python Regular Expressions

In the first two parts of this series, we looked at some fairly advanced usage of regular expressions. In this part, we take a step back and look at some of the other functions Python offers in the re module, then we talk about some common mistakes people regularly (ha!) make. Useful Python Regular Expression […]

Read More

Real-World Regular Expressions for Python

We’ve covered a lot of ground in this series of articles, so let’s now put it all together and work through a real-life application. A common task is to parse a Windows INI file, which are key/value pairs, separated into sections, something like this:

[Section 1]
val1=hello world
val2=42
[Section 2]
val1=foo!

Let’s first write a bit of Python code that reads […]

Read More

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK