๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
Remove List Duplicates Reverse a String Add Two Numbers ยท 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
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular Expression HOWTO โ€” Python 3.14.3 documentation
The r prefix, making the literal a raw string literal, is needed in this example because escape sequences in a normal โ€œcookedโ€ string literal that are not recognized by Python, as opposed to regular expressions, now result in a DeprecationWarning and will eventually become a SyntaxError.
Discussions

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
regex - Python re.search - Stack Overflow
I have a string variable containing string = "123hello456world789" string contain no spacess. I want to write a regex such that prints only words containing(a-z) I tried a simple regex p... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Probably the best tutorial on regular expressions I have ever read

Is that like "my best root canal ever"?

More on reddit.com
๐ŸŒ r/Python
53
376
December 4, 2013
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
4 days ago - For example, a*a will match 'aaaa' because the a* will match all 4 'a's, but, when the final 'a' is encountered, the expression is backtracked so that in the end the a* ends up matching 3 'a's total, and the fourth 'a' is matched by the final ...
๐ŸŒ
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 program will halt with an error.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ regular-expression-python-examples
Python RegEx - GeeksforGeeks
August 14, 2025 - The re module in Python provides various functions that help search, match, and manipulate strings using regular expressions. Below are main functions available in the re module: Let's see the working of these RegEx functions with definition and examples:
๐ŸŒ
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 - # Split the string into words using the regex separator words = re.split(sep_regex, s) # Filter out any empty strings from the result filtered_words = [word for word in words if word] # Print the resulting list of words print(filtered_words) # Output: ['we', 'did', 'Python', 'programs'] In this case, we use "\W+" as the separator, which matches one or more non-word characters, such as spaces, punctuation marks, etc. (i.e., characters that are not letters, digits, or underscores). Note that the last element of the split list might be an empty string, and we can remove it using the pop method, for example:
Find elsewhere
๐ŸŒ
LearnByExample
learnbyexample.github.io โ€บ python-regex-cheatsheet
Python regular expression cheatsheet and examples
June 9, 2025 - Overview and examples of Python regular expression syntax as implemented by the re built-in module
๐ŸŒ
Medium
medium.com โ€บ techtofreedom โ€บ 9-practical-examples-of-using-regular-expressions-in-python-1b4f8da5cdab
9 Practical Examples of Using Regular Expressions in Python | by Yang Zhou | TechToFreedom | Medium
April 13, 2023 - Python 9 Practical Examples of Using Regular Expressions in Python Handling text like a guru Whenever you meet problems with text manipulations, regular expressions (regex) are always your best โ€ฆ
๐ŸŒ
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
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ python-re-match
Python re.match() and re.sub() Explained | Built In
So, this example finds phone numbers in the 123-456-7890 format and converts them to (123) 456-7890. ... Contact me at (123) 456-7890 or (987) 654-3210. ... (): Used for capturing groups, allowing us to extract parts of the match and reference ...
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ mastering-python-regex-a-deep-dive-into-pattern-matching
Mastering Python RegEx: A Deep Dive into Pattern Matching - StrataScratch
July 21, 2023 - # Find all occurrences of 'Python' matches = re.findall(pattern, text) # Output the matches print(matches) Here is the output. The output shows that our code found all three character instances which start with โ€œpโ€ and ends with โ€œtโ€. The caret ^ is used to check if a string starts with a certain character. Letโ€™ see an example.
๐ŸŒ
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.
Top answer
1 of 2
22

re.search() finds the pattern once in the string, documenation:

Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

In order to match every occurrence, you need re.findall(), documentation:

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

Example:

>>> import re
>>> regex = re.compile(r'([a-z]+)', re.I)
>>> # using search we only get the first item.
>>> regex.search("123hello456world789").groups()
('hello',)
>>> # using findall we get every item.
>>> regex.findall("123hello456world789")
['hello', 'world']

UPDATE:

Due to your duplicate question (as discussed at this link) I have added my other answer here as well:

>>> import re
>>> regex = re.compile(r'([a-z][a-z-\']+[a-z])')
>>> regex.findall("HELLO W-O-R-L-D") # this has uppercase
[]  # there are no results here, because the string is uppercase
>>> regex.findall("HELLO W-O-R-L-D".lower()) # lets lowercase
['hello', 'w-o-r-l-d'] # now we have results
>>> regex.findall("123hello456world789")
['hello', 'world']

As you can see, the reason why you were failing on the first sample you provided is because of the uppercase, you can simply add the re.IGNORECASE flag, though you mentioned that matches should be lowercase only.

2 of 2
2

@InbarRose answer shows why re.search works that way, but if you want match objects rather than just the string outputs from re.findall, use re.finditer

>>> for match in re.finditer(pat, string):
...     print match.groups()
...
('hello',)
('world',)
>>>

Or alternatively if you wanted a list

>>> list(re.finditer(pat, string))
[<_sre.SRE_Match object at 0x022DB320>, <_sre.SRE_Match object at 0x022DB660>]

It's also generally a bad idea to use string as a variable name given that it's a common module.

๐ŸŒ
NTU Singapore
www3.ntu.edu.sg โ€บ home โ€บ ehchua โ€บ programming โ€บ howto โ€บ Regexe.html
Regular Expression (Regex) Tutorial
For examples, \+ matches "+"; \[ matches "["; and \. matches ".". Regex also recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode. $ python3 >>> import re # Need module 're' for regular expression # Try find: re.findall(regexStr, inStr) -> matchedStrList # r'...' denotes raw strings which ignore escape code, i.e., r'\n' is '\'+'n' >>> re.findall(r'a', 'abcabc') ['a', 'a'] >>> re.findall(r'=', 'abc=abc') # '=' is not a special regex character ['='] >>> re.findall(r'\.', 'abc.com') # '.' is a special regex character, need regex escape sequence ['.'] >>> re.findall('\\.', 'abc.com') # You need to write \\ for \ in regular Python string ['.']
๐ŸŒ
Machine Learning Plus
machinelearningplus.com โ€บ python โ€บ python-regex-tutorial-examples
Python Regular Expressions Tutorial and Examples: A Simplified Guide โ€“ Machine Learning Plus
January 20, 2018 - For example, \Btoy\B will match โ€˜toyโ€™ surrounded by words on both sides, as in, โ€˜antoynetโ€™. re.findall(r'\btoy\b', 'play toy broke toys') # match toy with boundary on both sides #> ['toy'] Letโ€™s get some practice. Itโ€™s time to open up your python console.
๐ŸŒ
Regex101
regex101.com
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ re-match-in-python
re.match() in Python - GeeksforGeeks
July 23, 2025 - string: This is the string you want to check for the pattern. re.match will try to match the pattern only at the beginning of the string. flags (optional): This is an optional parameter that allows you to modify how the matching should behave. For example, you can use re.IGNORECASE to make the matching case-insensitive.
๐ŸŒ
Medium
medium.com โ€บ @niraj.e21 โ€บ python101-regular-expression-mastering-python-regex-in-simple-steps-afd22449eafe
Python101 Regular Expression: Mastering Python RegEx in Simple Steps | by Niraj Tiwari | Medium
March 26, 2024 - For example: How can we split a string by any non-alphanumeric character? import re text = "Python,Java;C++|C#" pattern = "\W+" # \W matches any non-word character, + for one or more occurrences split_text = re.split(pattern, text) print("Split text:", split_text)
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