🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
Usually patterns will be expressed in Python code using this raw string notation. It is important to note that most regular expression operations are available as module-level functions and methods on compiled regular expressions. The functions are shortcuts that don’t require you to compile a regex ...
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
RegEx can be used to check if a string contains the specified search pattern. Python has a built-in package called re, which can be used to work with Regular Expressions.
Discussions

python - How can I remove text within parentheses with a regex? - Stack Overflow
I'm trying to handle a bunch of files, and I need to alter then to remove extraneous information in the filenames; notably, I'm trying to remove text inside parentheses. For example: filename = " 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
66
July 3, 2014
Python regular expressions, REGEX
Your regex is: First Name: (.*?) Last Name: (.*?) You are searching for the left-most match in the input: First Name: Gideon Last Name: Asiak So the regex engine consumes First Name:, then consumes as little as possible until Last Name: matches (saving Gideon in group 1), and then gets to match .*? against the remaining Asiak. As this is a non-greedy match, this pattern will consume as little as possible until we get a match. The pattern is already satisfied when consuming zero characters, so group 2 will contain the empty string. How to fix this: If you want to make sure that the entire string matches a pattern, use the fullmatch() function. Equivalently, you could anchor the pattern at the end of the string via the \z assertion. You could use a greedy match for the second group, e.g. (.*). It will consume as much as possible. In practice, if we can assume that each name won't contain spaces, I might write the pattern like this: First Name: (\S+) Last Name: (\S+). That is, use a more specific character class like \S (all non-space characters), and a quantifier that expects at least one character. More on reddit.com
🌐 r/learnpython
12
1
November 25, 2025
how hard is it to learn regex... is it worth learning?
Regex is a mini-programming language by itself. It is powerful and useful if you are doing a lot of text processing. Like a programming language, it will take time and practice to get comfortable with it. There are various online tools that can help you with learning, writing and debugging regex. regex101 — visual aid and online testing tool for regular expressions, select flavor as Python before use debuggex — railroad diagrams for regular expressions, select flavor as Python before use Other useful resources: Awesome Regex — curated collection of libraries, tools, frameworks and software PythonVerbalExpressions — construct regular expressions with natural language terms CommonRegex — collection of common regular expressions stackoverflow: regex FAQ More on reddit.com
🌐 r/learnpython
56
83
January 22, 2021
🌐
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 ...
🌐
Byu
labs.acme.byu.edu › DataScienceEssentials › RegularExpressions › RegularExpressions.html
Regular Expressions — ACME Labs
Along the way, we apply these techniques to a real data cleaning task. For reference throughout the lab, see the official Python documentation on regular expressions. A regular expression (or RegEx) is a string of characters that follows a certain syntax to specify a pattern, like generalized ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › regular-expression-python-examples
Python RegEx - GeeksforGeeks
August 14, 2025 - A Regular Expression or RegEx is a special sequence of characters that uses a search pattern to find a string or set of strings. It can detect the presence or absence of a text by matching it with a particular pattern and also can split a pattern ...
🌐
Medium
medium.com › @ebojacky › the-very-bare-minimum-essentials-for-regular-expressions-in-python-54e78c10b649
The Very Bare Minimum Essentials for Regular Expressions in Python | by Ebo Jackson | Medium
June 2, 2025 - Regular expressions (regex) in Python are a powerful tool for pattern matching, text manipulation, and data extraction. The re module provides a robust framework for working with regex, enabling developers to handle tasks like validation, parsing, ...
🌐
AppSignal
blog.appsignal.com › home › python › python regex: how to use re.search, re.match, and re.findall
Python Regex: How to Use re.search, re.match, and re.findall | AppSignal Blog
January 15, 2025 - Learn Python regex step by step: pattern basics, the re module's search, match, and findall functions, real-world examples, and performance tips.
Find elsewhere
🌐
YouTube
youtube.com › watch
Python Tutorial: re Module - How to Write and Match Regular Expressions (Regex) - YouTube
In this Python Programming Tutorial, we will be learning how to read, write, and match regular expressions with the re module. Regular expressions are extrem...
Published: October 24, 2017
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular expression HOWTO — Python 3.14.7 documentation
This document is an introductory tutorial to using regular expressions in Python with the re module. It provides a gentler introduction than the corresponding section in the Library Reference. Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized ...
🌐
YouTube
youtube.com › alex the analyst
Regular Expression Methods in Python - YouTube
Take my Full Python Course Here: https://bit.ly/48O581RIn this lesson we are going to look at Methods for Regular Expression in Python!GitHub Code: https://b...
Published: July 25, 2023
Views: 12K
🌐
Rexegg
rexegg.com › regex-python.php
Python Regex Tutorial
Python Regex Tutorial. Discusses the Python re and regex classes, provides working code for matching, replacing and splitting.
🌐
Mimo
mimo.org › glossary › python › regex-regular-expressions
Python Regex: Master Regular Expressions in Python
In Python, regular expressions (regex) allow you to search for and manipulate strings using specific patterns. Regex is a powerful tool for identifying, matching, and replacing substrings within longer texts.
Top answer
1 of 9
181
s/\([^)]*\)//

So in Python, you'd do:

re.sub(r'\([^)]*\)', '', filename)
2 of 9
140

The pattern that matches substrings in parentheses having no other ( and ) characters in between (like (xyz 123) in Text (abc(xyz 123)) is

\([^()]*\)

Details:

  • \( - an opening round bracket (note that in POSIX BRE, ( should be used, see sed example below)
  • [^()]* - zero or more (due to the * Kleene star quantifier) characters other than those defined in the negated character class/POSIX bracket expression, that is, any chars other than ( and )
  • \) - a closing round bracket (no escaping in POSIX BRE allowed)

Removing code snippets:

  • JavaScript: string.replace(/\([^()]*\)/g, '')
  • PHP: preg_replace('~\([^()]*\)~', '', $string)
  • Perl: $s =~ s/\([^()]*\)//g
  • Python: re.sub(r'\([^()]*\)', '', s)
  • C#: Regex.Replace(str, @"\([^()]*\)", string.Empty)
  • VB.NET: Regex.Replace(str, "\([^()]*\)", "")
  • Java: s.replaceAll("\\([^()]*\\)", "")
  • Ruby: s.gsub(/\([^()]*\)/, '')
  • R: gsub("\\([^()]*\\)", "", x)
  • Lua: string.gsub(s, "%([^()]*%)", "")
  • sed: sed 's/([^()]*)//g'
  • Tcl: regsub -all {\([^()]*\)} $s "" result
  • C++ std::regex: std::regex_replace(s, std::regex(R"(\([^()]*\))"), "")
  • Objective-C:
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^()]*\\)" options:NSRegularExpressionCaseInsensitive error:&error]; NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
  • Swift: s.replacingOccurrences(of: "\\([^()]*\\)", with: "", options: [.regularExpression])
  • Google BigQuery: REGEXP_REPLACE(col, "\\([^()]*\\)" , "")
🌐
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
🌐
Reddit
reddit.com › r/learnpython › python regular expressions, regex
r/learnpython on Reddit: Python regular expressions, REGEX
November 25, 2025 -

Hello my friend! I am learning python using the popular book, Automate the boring stuff book and I came accross the regeneration class. I tried non-greedy matching the two groups of characters in a string. The group method returned the first group but didnt the second group. I asked chat gpt and it said my code is fine. It gave me some probable causes pf such an issue that there us a newline but that isn't so. Attached is my code.

Will appreciate your assistance and comments. Thank you

  1. name_regex1 = re.compile(r"First Name: (.?) Last Name: (.?)")

  2. name2 = name_regex1.search("First Name: Gideon Last Name: Asiak")

  3. print(name2.group(2))

Sorry I couldn't attach the screenshot, but this is the code up here.(please know that there are no newline, each statement is in its line)

NOTE: there is an asterisk between the '.' and '?'. I dont know why when I post it dissapears.

Top answer
1 of 4
6
Your regex is: First Name: (.*?) Last Name: (.*?) You are searching for the left-most match in the input: First Name: Gideon Last Name: Asiak So the regex engine consumes First Name:, then consumes as little as possible until Last Name: matches (saving Gideon in group 1), and then gets to match .*? against the remaining Asiak. As this is a non-greedy match, this pattern will consume as little as possible until we get a match. The pattern is already satisfied when consuming zero characters, so group 2 will contain the empty string. How to fix this: If you want to make sure that the entire string matches a pattern, use the fullmatch() function. Equivalently, you could anchor the pattern at the end of the string via the \z assertion. You could use a greedy match for the second group, e.g. (.*). It will consume as much as possible. In practice, if we can assume that each name won't contain spaces, I might write the pattern like this: First Name: (\S+) Last Name: (\S+). That is, use a more specific character class like \S (all non-space characters), and a quantifier that expects at least one character.
2 of 4
3
Hey there! The regeneration regular expressions (regex) library lets you use patterns (regular expressions) to search for matches in a piece of text. Your regular expression r'First Name: (.?) Last Name: (.?) is close, but not quite correct. To find the names 'Gideon' and 'Asiak', replace the ? with a +. (): Create a pattern matching group .: Match any character +: Match any length from re import compile name_regex1 = compile(r'First Name: (.+) Last Name: (.+)') name2 = name_regex1.search('First Name: Gideon Last Name: Asiak') print(name2.group(1)) # 'Gideon' print(name2.group(2)) # 'Asiak'
🌐
DataCamp
datacamp.com › cheat-sheet › regular-expresso
Regex Cheat Sheet — Regular Expressions in Python | DataCamp
October 5, 2022 - Discover the power of Regular Expressions (RegEx) for pattern matching in Excel. Our comprehensive guide unveils how to standardize data, extract keywords, and perform advanced text manipulations. ... Discover the power of regular expressions with this tutorial. You will work with the re library, deal with pattern matching, learn about greedy and non-greedy matching, and much more! ... This tutorial takes course material from DataCamp's Cleaning Data in Python course and allows you to clean strings using regular expressions.
🌐
Medium
medium.com › geoblinktech › so-a-few-months-ago-i-had-to-search-the-quickest-way-to-apply-a-regular-expression-to-a-huge-c0883f8d4e4f
How to write efficient Regular Expressions in Python | by Denis Vivies | Geoblink Tech blog | Medium
November 30, 2018 - Regular expressions (also called regexp) are chains of characters that define a search pattern (thanks wikipedia!). Basically, it’s a tool that allows you to filter, extract or transform a chain of characters.
🌐
LabEx
labex.io › home › cheatsheet › regular expressions
Python Regular Expressions - Python Cheat Sheet
A regular expression (shortened as regex [...]) is a sequence of characters that specifies a search pattern in text.
🌐
Better Programming
betterprogramming.pub › the-hitchhikers-guide-to-regular-expressions-and-python-s-re-library-1342444900d2
The Hitchhiker’s Guide to Regular Expressions and Python’s re Library | by Hannah Parker | Better Programming
September 29, 2019 - There are two parts of this synthesis: regular expressions and Python’s re library. I separate these because regexes are a cross-language tool, and Python’s re library is a very common Python-specific implementation of this tool.
🌐
IONOS
ionos.com › digital guide › websites › web development › python regex
How to use Python RegEx - IONOS
July 21, 2023 - The findall() function is probably the most important function when using Python RegEx . It takes a search pattern and a Python string and returns a Python list. The list consists of strings con­tain­ing all matches in the order that they were found.