software library for interpreting regular expressions
Perl Compatible Regular Expressions (PCRE) is a library written in C, which implements a regular expression engine, inspired by the capabilities of the Perl programming language. Philip Hazel started writing PCRE in … Wikipedia
Factsheet
Original author Philip Hazel
Written in C
Operating system Cross-platform
Factsheet
Original author Philip Hazel
Written in C
Operating system Cross-platform
🌐
Wikipedia
en.wikipedia.org › wiki › Perl_Compatible_Regular_Expressions
Perl Compatible Regular Expressions - Wikipedia
January 8, 2026 - For example, (a|b)c\1 would match either "aca" or "bcb" and would not match, for example, "acb". A sub-pattern (surrounded by parentheses, like (...)) may be named by including a leading ?P<name> after the opening parenthesis. Named subpatterns are a feature that PCRE adopted from Python regular expressions.
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
Discussions

Python: How to use RegEx in an if statement? - Stack Overflow
I have the following code which looks through the files in one directory and copies files that contain a certain string into another directory, but I am trying to use Regular Expressions as the st... More on stackoverflow.com
🌐 stackoverflow.com
Explain Like I'm 5: Regular Expressions
IMHO making eyes glaze over is what regular expressions excels at. I hate it with a passion. That being said, it is a powerful, useful tool for parsing text. The key thing that triggered my understanding of regex is that all characters/arguments/etc are positional. They aren't flags, triggers, or what have you, they match the literal of what they represent at that position in your regex string. So, a quick example. I use this in a script of mine for grabbing a version string. "^[0-9]+\.[0-9]+\.[0-9]+" It literally matches start of line, one or more of the digits of zero through nine, a period, one or more digits of zero through nine, a period, one or more digits of zero through nine The carat ^ matches the literal start of line. [0-9] is a kind of specified wildcard, it says the character here can match any digit zero through nine. + is a special character that extends the previous argument in the string ([0-9]) and says to match it at least once and then for as many times as it positively matches. \ is the escape character, it says 'treat the following argument as a string literal, not as a special character'. . since it was escaped by \, we are looking for a literal period/decimal-point. From there, the sequence repeats. It will match any of these, and more: 1.0.5, 15.154.42, 0.0.5 I'm not on my computer with my scripts so I don't have the actual function it's used in handy. I really struggled hard to wrap my head around regular expressions, and I really hope this helps. If you want, I'll come back later when I have access to my scripts and post some actual in-use functions. More on reddit.com
🌐 r/learnpython
43
65
July 3, 2014
Python re.sub() regex to substitute a character with a string except in quotes
The third-party regex module is useful for such cases: >>> import regex >>> s1 = '@ foo@ "abc@google.com"' >>> s2 = "@ foo@ 'abc@google.com'" >>> regex.sub(r'''(?:'[^']+'|"[^"]+")(*SKIP)(*F)|@''', 'bar', s1) 'bar foobar "abc@google.com"' >>> regex.sub(r'''(?:'[^']+'|"[^"]+")(*SKIP)(*F)|@''', 'bar', s2) "bar foobar 'abc@google.com'" Otherwise, you can use a custom function in the replacement section: >>> import re >>> s1 = '@ foo@ "abc@google.com"' >>> s2 = "@ foo@ 'abc@google.com'" >>> re.sub(r'''@|'[^']+'|"[^"]+"''', lambda m: 'bar' if m[0] == '@' else m[0], s1) 'bar foobar "abc@google.com"' >>> re.sub(r'''@|'[^']+'|"[^"]+"''', lambda m: 'bar' if m[0] == '@' else m[0], s2) "bar foobar 'abc@google.com'" More on reddit.com
🌐 r/regex
12
5
August 14, 2021
[Python] How to get a RegEx code to grab a Stock Name using the Ticker
You've gotten the start of the name consistently, yes? So instead of [^\s]+ (meaning "get characters until a whitespace is found'), you can just use [^)]+ ('Get characters until a closing parenthesis is found') More on reddit.com
🌐 r/AskProgramming
6
1
December 31, 2020
🌐
DaniWeb
daniweb.com › programming › software-development › tutorials › 238544 › simple-regex-tutorial
python - Simple Regex tutorial | DaniWeb
November 15, 2009 - This will not get all email addresses its just a simple example designed to show people the possibilities of the regex module. If you want to extend yourself in this, try making it so that is accepts .org/.net/com.au etc. Hope you enjoyed the tutorial and learnt something :) ... I am a 17 year old High school student. I live in Newcaslte, Australia. I started learning python ...
🌐
Programiz
programiz.com › python-programming › regex
Python RegEx (With Examples)
If you already know the basics of RegEx, jump to Python RegEx. To specify regular expressions, metacharacters are used. In the above example, ^ and $ are metacharacters.
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
\d -- decimal digit [0-9] (some older regex utilities do not support \d, but they all support \w and \s) ^ = start, $ = end -- match the start or end of the string · \ -- inhibit the "specialness" of a character. So, for example, use \. to match a period or \\ to match a slash. If you are unsure if a character has special meaning, such as '@', you can try putting a slash in front of it, \@. If its not a valid escape sequence, like \c, your python ...
🌐
Real Python
realpython.com › regex-python
Regular Expressions: Regexes in Python (Part 1) – Real Python
October 21, 2023 - For example, rather than searching for a fixed substring like '123', suppose you wanted to determine whether a string contains any three consecutive decimal digit characters, as in the strings 'foo123bar', 'foo456bar', '234baz', and 'qux678'. Strict character comparisons won’t cut it here. This is where regexes in Python come to the rescue.
Find elsewhere
🌐
Medium
medium.com › @tubelwj › commonly-used-python-regular-expression-examples-065526b5b8b5
Commonly Used Python Regular Expression Examples | by Gen. Devin DL. | Medium
January 2, 2025 - Here, we use “programmer” as the old content and “Python” as the new content, replacing “programmer” with “Python” in the string s. We can also use sub-expressions in regular expressions for more complex replacements, such as: import re # Define the old regex pattern for matching two words old_regex = r"(\w+) (\w+)" # Captures two words separated by a space # Define the new format for swapping the two captured words new_format = r"\2 \1" # Replaces the first word with the second and vice versa # Original string s = "Hello programmer" # Perform substitution using re.sub to swap the words s = re.sub(old_regex, new_format, s) # Print the result of the substitution print(s) # Output: programmer Hello
🌐
GeeksforGeeks
geeksforgeeks.org › python › regular-expression-python-examples
Python RegEx - GeeksforGeeks
August 14, 2025 - import re regex = r"([a-zA-Z]+) (\d+)" match = re.search(regex, "I was born on June 24") if match: print("Match at index %s, %s" % (match.start(), match.end())) print("Full match:", match.group(0)) print("Month:", match.group(1)) print("Day:", match.group(2)) else: print("The regex pattern does not match.") ... Metacharacters are special characters in regular expressions used to define search patterns. The re module in Python supports several metacharacters that help you perform powerful pattern matching. ... The backslash (\) makes sure that the character is not treated in a special way. This can be considered a way of escaping metacharacters. For example, if you want to search for the dot(.) in the string then you will find that dot(.) will be treated as a special character as is one of the metacharacters (as shown in the above table).
🌐
Dataquest
dataquest.io › blog › regular-expressions-data-scientists
Tutorial: Python Regex (Regular Expressions) for Data Scientists
April 9, 2023 - Perhaps the only puzzler here is the regex pattern, \d+\s\w+\s\d+. The date starts with a number. Hence, we use d to account for it. However, as the DD part of the date, it could be either one or two digits. Here is where + becomes important. In Python regex, + matches 1 or more instances of a pattern on its left.
🌐
Medium
medium.com › pythons-gurus › mastering-regular-expressions-in-python-fa1020f7ea78
Regular Expressions in Python | Python’s Gurus
July 11, 2024 - Master Python regex with our guide. Learn pattern matching, special characters, and grouping techniques to enhance your text processing skills.
Top answer
1 of 6
160
import re
if re.match(regex, content):
  blah..

You could also use re.search depending on how you want it to match.

You can run this example:

"""
very nice interface to try regexes: https://regex101.com/
"""
# %%
"""Simple if statement with a regex"""
import re

regex = r"\s*Proof.\s*"
contents = ['Proof.\n', '\nProof.\n']
for content in contents:
    assert re.match(regex, content), f'Failed on {content=} with {regex=}'
    if re.match(regex, content):
        print(content)
2 of 6
59

if re.search(r'pattern', string):

Simple if-regex example:

if re.search(r'ing\b', "seeking a great perhaps"):     # any words end with ing?
    print("yes")

Complex if-regex example (pattern check, extract a substring, case insensitive):

search_object = re.search(r'^OUGHT (.*) BE$', "ought to be", flags=re.IGNORECASE)
if search_object:
    assert "to" == search_object.group(1)     # what's between ought and be?

Notes:

  • Use re.search() not re.match. The match method restricts to the start of the string, a confusing convention. If you want that, search explicitly with caret: re.search(r'^...', ...) (Or in re.MULTILINE mode use \A)

  • Use raw string syntax r'pattern' for the first parameter. Otherwise you would need to double up backslashes, as in re.search('ing\\b', ...)

  • In these examples, '\\b' or r'\b' is a special sequence meaning word-boundary for regex purposes. Not to be confused with '\b' or '\x08' backspace.

  • re.search() returns None if it doesn't find anything, which is always falsy.

  • re.search() returns a Match object if it finds anything, which is always truthy.

  • even though re.search() returns a Match object (type(search_object) is re.Match) I have taken to calling the return value a search_object. I keep returning to my own bloody answer here because I can't remember whether to use match or search. It's search, dammit.

  • a group is what matched inside pattern parentheses.

  • group numbering starts at 1.

  • Specs

  • Tutorial

🌐
No Starch Press
nostarch.com › automate-boring-stuff-python-3rd-edition
Automate the Boring Stuff with Python, 3rd Edition | No Starch Press
July 11, 2023 - In this fully revised third edition of Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Early chapters will teach you the fundamentals of Python through clear explanations and engaging examples.
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
4 days ago - An enum.IntFlag class containing the regex options listed below. ... Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode (str) patterns, and is ignored for bytes patterns. Corresponds to the inline flag (?a). ... The U flag still exists for backward compatibility, but is redundant in Python 3 since matches are Unicode by default for str patterns, and Unicode matching isn’t allowed for bytes patterns.
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular Expression HOWTO — Python 3.14.3 documentation
However, if that was the only additional capability of regexes, they wouldn’t be much of an advance. Another capability is that you can specify that portions of the RE must be repeated a certain number of times. The first metacharacter for repeating things that we’ll look at is *. * doesn’t match the literal character '*'; instead, it specifies that the previous character can be matched zero or more times, instead of exactly once. For example, ca*t will match 'ct' (0 'a' characters), 'cat' (1 'a'), 'caaat' (3 'a' characters), and so forth.
🌐
Reddit
reddit.com › r/learnpython › explain like i'm 5: regular expressions
r/learnpython on Reddit: Explain Like I'm 5: Regular Expressions
July 3, 2014 -

Could someone please explain regular expressions and how they're used?

Every tutorial I've read online spends a lot of time going over special characters until I glaze over. After reading a bunch, I know what the special characters are, but not why/how to use them.

Could you include a simple function that illustrates?

Thank you

Top answer
1 of 5
35
IMHO making eyes glaze over is what regular expressions excels at. I hate it with a passion. That being said, it is a powerful, useful tool for parsing text. The key thing that triggered my understanding of regex is that all characters/arguments/etc are positional. They aren't flags, triggers, or what have you, they match the literal of what they represent at that position in your regex string. So, a quick example. I use this in a script of mine for grabbing a version string. "^[0-9]+\.[0-9]+\.[0-9]+" It literally matches start of line, one or more of the digits of zero through nine, a period, one or more digits of zero through nine, a period, one or more digits of zero through nine The carat ^ matches the literal start of line. [0-9] is a kind of specified wildcard, it says the character here can match any digit zero through nine. + is a special character that extends the previous argument in the string ([0-9]) and says to match it at least once and then for as many times as it positively matches. \ is the escape character, it says 'treat the following argument as a string literal, not as a special character'. . since it was escaped by \, we are looking for a literal period/decimal-point. From there, the sequence repeats. It will match any of these, and more: 1.0.5, 15.154.42, 0.0.5 I'm not on my computer with my scripts so I don't have the actual function it's used in handy. I really struggled hard to wrap my head around regular expressions, and I really hope this helps. If you want, I'll come back later when I have access to my scripts and post some actual in-use functions.
2 of 5
12
Regular expressions are used to find lines or specific sections of text that follow some basic rules, but might have dynamic content. For example, say you have a list of photo file names. They all start with IMG and have the date, formatted as YYYY-MM-DD, followed by the time as HHMMSS, all separated by underscores, followed by the file extension, .jpg: IMG_YYYY-MM-DD_HHMMSS.jpg Now, you want to find all pictures taken in January and August of 2012. MM will either be 01 or 08. YYYY will be 2012. D, H, M and S will all be numbers. ^IMG_2012-0[18]-[0-9]{2}_[0-9]{6}\.jpg$ We are looking for lines that start with "IMG_2012-0" and end with ".jpg", having the following between them (left to right) a number that is either 1 or 8 another dash, followed by 2 numbers and an underscore 6 more numbers When you apply this regex to a block of text that has one filename on each line, it should return all the filenames that indicate the photo was taken in January or August of 2012. There are a lot of regex tester tools on the web. My current favorite is Regexr. Find one that seems easy to use, and experiment with different text contents and regexes. This is a good way to see how regexes can be used and get some practice applying the different rules used in regular expressions. Regexr loads with a block of different kinds of text that are commonly filtered for using regexes--phone numbers, coordinates, addresses, currency, etc. So that's why I recommend it. edit: To put this in python, it might look like this: import re regex = re.compile('^IMG_2012-0[18]-[0-9]{2}_[0-9]{6}\.jpg$') with open('photolist.txt') as photo_list: results = regex.findall(photo_list.readlines()) # findall() will return a list of every match found for result in results: print(result) # print each match on a line in stdout
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
Arguments read from a file must be one per line by default (but see also convert_arg_line_to_args()) and are treated as if they were in the same place as the original file referencing argument on the command line. So in the example above, the expression ['-f', 'foo', '@args.txt'] is considered equivalent to the expression ['-f', 'foo', '-f', 'bar'].
🌐
Semantic Versioning
semver.org
Semantic Versioning 2.0.0 | Semantic Versioning
Example: git tag v1.2.3 -m "Release version 1.2.3", in which case “v1.2.3” is a tag name and the semantic version is “1.2.3”. There are two. One with named groups for those systems that support them (PCRE [Perl Compatible Regular Expressions, i.e. Perl, PHP and R], Python and Go).
🌐
Django
docs.djangoproject.com › en › 6.0 › topics › db › models
Models | Django documentation | Django
The last method in this example is a property. The model instance reference has a complete list of methods automatically given to each model. You can override most of these – see overriding predefined model methods, below – but there are a couple that you’ll almost always want to define: ... A Python “magic method” that returns a string representation of any object.