Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

How Not to Match a Character After Repetition in Python Regex



Regular expression, or regex, is a useful tool in Python that helps identifying and processing of text patterns. It helps you cut, edit and check text in different ways. In this article, we will talk about how not to match a character after repetition in regex and look at five different examples. We will provide you a clear explanation to help you understand how each one works.

All of the examples we will create in this article, we will use the re.findall() method, which is a built-in method of Python's re module. Let us know about this function first -

Using re.findall() Function

In this article, we will be using re.findall() function which is used to search for all occurrences of a pattern in the given string. It basically returns a list of all matches found.

The function accepts two parameters: the pattern to search for, and the string to search in. The pattern can have special characters and syntax to define what you are looking for.

syntax

Here is the use of re.findall() method of the Python re module -

#First import the re module   
import re

re.findall(pattern, string)

Now let us see different examples to show how not to match a character after repetition -

Example: Positive Lookahead

In this example, we have used x as the character to be matched. And we are looking for one or more digits followed by an 'x'. In the pattern, we have used '(?=x)', which is a positive lookahead to check if the next character is 'x' without including it in the match.

# Import the re module
import re

# Using positive lookahead to match 'x'
pat = r'(\d)+(?=x)'
text = '123x 456xx 789xxx'

matches = re.findall(pat, text)
print(matches)

Output

Here is the output of the above program -

['3', '6', '9']

Example: Negative Lookahead

In this example, we are using negative lookahead to exclude 'x' from the match. The pattern '(?!x)' is used to check if the next character is not 'x'. So, we are looking for one or more digits that are not followed by 'x'.

# Import the re module
import re

# Using negative lookahead to exclude 'x'
txt = "aaaaX aaaY aaaaZ aaaX"
pat = r"a{3,4}(?!X)"  

print(re.findall(pat, txt))

Output

After running the program, you will get this result -

['aaa', 'aaa', 'aaaa']

Example: Match Number

In the following example, we will find three-digit numbers in which we will match numbers but ignore the '5' after repeated digits. See the below example -

# Import the re module
import re

txt = "777A 7775 888C 888B"

# Match three digits 
pat = r"\d{3}(?!5)"  

print(re.findall(pat, txt)) 

Output

This output will be displayed when the program runs -

['777', '775', '888', '888']

Example: Avoid matching '!'

In the below example, we are using the re.findall() method to find words of 4+ letters and skip matches if there is an exclamation mark '!' that follows.

# Import the re module
import re

txt = "Wow! Amazing Fantastic! Excellent"

# Match words with 4+ letters 
pat = r"\b\w{4,}(?!\!)"  

print(re.findall(pat, txt))

Output

You will see this result after executing the program -

['Amazing', 'Fantasti', 'Excellent']
Updated on: 2025-05-26T18:44:05+05:30

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements