From the documentation of re.findall:

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.

While your regexp is matching the string three times, the (.*?) group is empty for the second two matches. If you want the output of the other half of the regexp, you can add a second group:

>>> re.findall(r'\((.*?)\)|(\w)', '(zyx)bc')
[('zyx', ''), ('', 'b'), ('', 'c')]

Alternatively, you could remove all the groups to get a simple list of strings again:

>>> re.findall(r'\(.*?\)|\w', '(zyx)bc')
['(zyx)', 'b', 'c']

You would need to manually remove the parentheses though.

Answer from James Henstridge on Stack Overflow
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex replace multiple patterns
Python regex replace multiple patterns - Spark By {Examples}
May 31, 2024 - These patterns are combined using the | operator within parentheses to create the pattern group. The re.sub() method is then used to replace all occurrences of any of these patterns with the string "hello world".
Discussions

Handling multiple regex patterns when looking through a dataframe?
Hm, I got it working on a toy example. I'll give you a couple of hints: Are you able to put all of the 5000 schools into a single combined capture group like so: r"(main|secondary|primary)" If you can: the .findall() method will return a list into a new dataframe column, like so: >>> df col newcol 0 main primary [main, primary] 1 secondary [secondary] 2 primary [primary] 3 this should not match [] More on reddit.com
🌐 r/learnpython
11
1
September 27, 2021
Having a logical and for multiple regex patterns - Python
Something like this? (?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])^[a-zA-Z0-9]{6,}$ The check for criteria you need to use a "Look ahead". In Regex a positive look ahead (?=) does NOT move the cursor (where the match is actually starting) but looks aheads for a match. You have three criteria so 3 look aheads. The .* in the look ahead tells regex to look for the following character class [0-9], [a-z] or [A-Z] anywhere in the match. It must find each one at least once. Finally the actual match is valid ONLY if it consists of the specified classes and is 6+ characters long. Regex101 Example Source More on reddit.com
🌐 r/regex
5
1
September 15, 2022
Combine multiple regex pattern module into 1
Hey, I have an automation that uses multiple regex patterns to extract specific details such as name, date etc. Is there a way I could combine all the regex pattern module into one? The current one works perfectly but looks a bit cluttered Here are the regex patterns used in each module ([^|\n]+) ... More on community.make.com
🌐 community.make.com
0
0
January 27, 2023
How to combine multiple regex into single one in python? - Stack Overflow
If you need to squash multiple regex patterns together the result can be annoying to parse--unless you use P and .groupdict() but doing that can be pretty verbose and hacky. More on stackoverflow.com
🌐 stackoverflow.com
🌐
LearnByExample
learnbyexample.github.io › py_regular_expressions › alternation-and-grouping.html
Alternation and Grouping - Understanding Python re(gex)?
Similarly, in regular expressions, you can use the | metacharacter to combine multiple patterns to indicate logical OR. The matching will succeed if any of the alternate pattern is found in the input string.
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node84.html
Multiple Matches
We've already seen that the findall method can return a list containing multiple occurrences of a match within a string. There are a few subtleties in the use of findall which should be mentioned, as well as alternatives which may be useful in certain situations. One consideration about findall is that if there are tagged subexpressions within the regular expression, findall returns a list of tuples containing all of the tagged expressions. For example, consider matching a pattern consisting of a number followed by a word.
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch03s15.html
Replacing Multiple Patterns in a Single Pass - Python Cookbook [Book]
July 19, 2002 - # requires Python 2.1 or later from _ _future_ _ import nested_scopes import re # the simplest, lambda-based implementation def multiple_replace(adict, text): # Create a regular expression from all of the dictionary keys regex = re.compile("|".join(map(re.escape, adict.keys( )))) # For each match, look up the corresponding value in the dictionary return regex.sub(lambda match: adict[match.group(0)], text) A more powerful and flexible approach is to wrap the dictionary into a callable object that directly supports the lookup and replacement idea, which you can use directly as the callback in the sub method.
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
Reddit
reddit.com › r/learnpython › handling multiple regex patterns when looking through a dataframe?
r/learnpython on Reddit: Handling multiple regex patterns when looking through a dataframe?
September 27, 2021 -

I have a list of about 5000 schools. I want to iterate through this list and use each one as a regex search pattern. If the pattern is found when iterating through the dataframe column, I want to put that value in a new column. The issue is, there is sometimes more than 1 matching pattern and I want to see all of them incase one is a better match. My current code will either overwrite the pattern or use the first one found.

Posts['post_mod'] is the dataframe column I am iterating over and schools is a list.

index_posts = posts.columns.get_loc('post_mod')
index_schools = schools.columns.get_loc('Input')
for school in tqdm(schools_list):
    school_pattern = rf'{school}'
    for row in range(len(posts)):
        try:
            school = re.search(school_pattern, posts.iat[row, index_posts]).group()
        except:
            continue
        posts.loc[row, index_posts] = school

final = pd.merge(posts, schools, how='left', left_on=6, right_on='Input')
final = final.drop_duplicates().reset_index(drop=True)
🌐
LabEx
labex.io › tutorials › python-how-to-perform-multiple-regex-substitutions-467215
How to perform multiple regex substitutions | LabEx
By mastering these basics, you'll be well-prepared to perform complex text manipulations with Python's regex capabilities in LabEx environments. Regular expression substitution allows you to replace text patterns efficiently using Python's re module. import re ## Basic string replacement text = "Hello, LabEx is awesome programming platform" result = re.sub(r"LabEx", "Python Learning", text) print(result) ## Output: Hello, Python Learning is awesome programming platform · def multiple_replacements(text): ## Define replacement dictionary replacements = { r'\bpython\b': 'Python', r'\blinux\b': '
Find elsewhere
🌐
Orel Fichman
orelfichman.com › blog › multiple-regex-checking-python-one-liner
Multiple-regex-checking Python One-liner | Orel Fichman
March 14, 2020 - >>> password = "Hello" >>> password_regex = [lowercase_regex, uppercase_regex, numbers_regex, special_regex] >>> password_regex_matches_list = list(map(lambda regex: re.findall(regex, password), password_regex)) >>> password_regex_matches_list [['e', 'l', 'l', 'o'], ['H'], [], []]
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
1 week ago - The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pattern-matching-python-regex
Pattern matching in Python with Regex - GeeksforGeeks
January 19, 2026 - The (wo)? part of the regular expression means that the pattern wo is an optional group. The regex will match text that has zero instances or one instance of wo in it. This is why the regex matches both 'Batwoman' and 'Batman'. You can think of the ? as saying,groups “Match zero or one of the group preceding this question mark.” If you need to match an actual question mark character, escape it with \?. ... # Python program to illustrate # optional matching # with question mark(?) import re batRegex = re.compile(r'Bat(wo)?man') mo1 = batRegex.search('The Adventures of Batman') mo2 = batRegex.search('The Adventures of Batwoman') print(mo1.group()) print(mo2.group())
🌐
YouTube
youtube.com › codetime
python regex match multiple patterns - YouTube
Download this code from https://codegive.com Regular expressions, commonly known as regex or regexp, provide a powerful and flexible way to search, match, an...
Published   January 21, 2024
Views   29
🌐
Reddit
reddit.com › r/regex › having a logical and for multiple regex patterns - python
r/regex on Reddit: Having a logical and for multiple regex patterns - Python
September 15, 2022 -

Hey guys, I would consider myself a beginner in regex and was given a challenge (by codewars) which I would like to learn about (googling did not help):

I have several (4) simple patterns that all need to be given at the same time. To be specific I receive a string and should check whether it

  1. contains at least 6 (only alpha-numeric) characters -> [a-zA-Z0-9]{6,}

  2. at least 1 upper case letter -> [A-Z]{1,}

  3. at least 1 lower case letter -> [a-z]{1,}

  4. at least 1 number -> [0-9]{1,}

However, all requirements need to be checked in a single pattern. Basically I want to exchange the logical or (| -> "[a-zA-Z0-9]{6,}|[A-Z]{1,}|[a-z]{1,}|[0-9]{1,}) for an and :)

Can you help me learn? :)

Examples:
fjd3IR9 -> true
ghdfj32 -> false (no upper case)
fjd3 IR9 -> false (white space is not alpha numeric)
djI38D55 -> true

🌐
PYnative
pynative.com › home › python › regex › python regex search using re.search()
Python Regex Search using re.search()
April 2, 2021 - On the other hand, the findall() method returns all the matches in the form of a Python list. In this section, we will learn how to search for multiple distinct patterns inside the same target string.
🌐
Medium
toltman.medium.com › matching-multiple-regex-patterns-in-pandas-121d6127dd47
Matching Multiple Regex Patterns in Pandas | by Tim Oltman | Medium
February 14, 2021 - It will return True if our pattern is found at any position in the string. ... Now we get get a match for every string! Note that we would also get a match if our string contained any combination of 4 digits in a row. Even if it were something like 12345.45. We want to extract these values from our strings so let’s try the Series.str.extract method. ... Note that we’ve added parentheses to our regex pattern.
🌐
Make Community
community.make.com › questions
Combine multiple regex pattern module into 1 - Questions - Make Community
January 27, 2023 - Hey, I have an automation that uses multiple regex patterns to extract specific details such as name, date etc. Is there a way I could combine all the regex pattern module into one? The current one works perfectly but looks a bit cluttered Here are the regex patterns used in each module ([^|\n]+) | ([^\s@]+@[^\s@]+) | (\d+) Location:\s(.+) Case Lead ID: #(.+) Case Description:\s±\s*(.+)
🌐
PYnative
pynative.com › home › python › regex › python regex capturing groups
Python Regex Capturing Groups – PYnative
April 12, 2021 - Python regex capturing groups match several distinct patterns inside the same target string using group() and groups()
🌐
Finxter
blog.finxter.com › home › learn python blog › how to access multiple matches of a regex group in python?
How to Access Multiple Matches of a Regex Group in Python? - Be on the Right Side of Change
April 3, 2023 - In this example, I use a regex pattern with a single capturing group to match words (using the \b word boundary anchor). The \1 syntax refers to the text matched by the first group, allowing us to find consecutive occurrences of the same word. The re.IGNORECASE flag ensures case-insensitive matching. So, no repeated word can escape my Python regex magic!✨ · In this article, I discussed how to access multiple matches of a regex group in Python.
🌐
PythonAnywhere
pythonanywhere.com › forums › topic › 29845
Run multiple regex (find and replace) in files : Forums : PythonAnywhere
import os import re path = "D:/11" def listdir(dir): filenames = os.listdir(dir) for files in filenames: print(files) listdir(path) extensie_fisier = ".html" regexes = { r'<title>.*\|\K(.*)(</title>)': r'Bebe', # First regex r'regex1': r'string1' r'^<html.*$': r'yes' # Second regex r'regex1': r'string1' } for k, v in regexes.items(): print(re.sub(k, v, 'text')) with open("*.html", mode="w") as file: # See note 2. contents = file.write(f"{extensie_fisier.text}") print("DONE !") deleted-user-10711132 | 11 posts | June 3, 2021, 2:23 p.m. | permalink · You need to import re if you want to use it. glenn | 10420 posts | PythonAnywhere staff | June 3, 2021, 3:32 p.m.