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

Specify Repetitions in Regex using Python



A regular expression is a series of characters that allows you to discover a string or a set of strings using a search pattern. Regular expressions are sometimes known as RegEx. In Python, the re module is used to work with regular expressions.

We can use repetitions in regular expression to match the string in python. To make repetitions possible in regular expression, we indicate the number of times the character is repeated in {}.

Understanding Repetitions in Regex

Regex allows you to specify the number of times a pattern should appear by using various special characters. The definitions of repetitions that are most frequently used are as follows -

  • Asterisk *: Shows that the previous element appears zero or more times.

  • Plus +: Matches to one or more iterations of the element that comes before it.

  • Mark, question?: Refers to either one or zero iterations of the element that comes before it.

  • Curly Braces {}: Related to a predetermined quantity of instances. As an example, {2,4} indicates between two and four occurrences, while {2} indicates precisely two occurrences.

Using Asterisk " * "

The asterisk symbol (*) is used for matching a pattern that can occur any number of times, including not at all.

Example

In the following example, we demonstrate the usage of the asterisk (*) operator to match zero or more occurrences of the character 'a' in the given string. The re.findall() function is used to find all matches of the pattern in the string and return them as a list.

import re

pattern = r'a*'  # Matches 'a' zero or more times
text = "aaabbaaa"

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

Here is the output obtained on executing the above program -

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

Using Plus " + "

The plus (+) makes sure that the previous element appears at least once.

Example

The following example demonstrates the usage of the plus (+) operator to match one or more occurrences of 'a'.

import re

# Matches 'a' one or more times
pattern = r'a+' 
text = "aaabbaaa"

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

Here is the output obtained on executing the above program -

['aaa', 'aaa']

Using Question Mark " ? "

The question mark (?) is used to match the preceding element zero or one time.

Example

In the following example, we match 'a' which is present in the string 'aaabbaaa'. Here, we have used a "?" to match the required string.

import re

# Matches 'a' zero or one time
pattern = r'a?'  
text = "aaabbaaa"

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

Here is the output obtained on executing the above program -

['a', 'a', 'a', '', '', 'a', 'a', 'a', '']

Using Curly Braces " {} "

Curly braces allow for a precise definition of how many times a pattern should repeat. In the following example, we match 'TTTT' string, which is present in the string 'TTTTTTTTTT'. Here, we have used T{4} to match the required string.

Using search() function

In the following example, we match 'TTTTPPP' string, which is present in the string 'TTTTTTPPPPPPPPPPPP'. Here, we have used T{4}P{3} to match the required string.

Example

The following example is a program that shows how to specify repetitions Regex in Python using search() function. We begin by importing the regular expression module.

Then, we have used the search() function, which is imported from the re module, to get the required string. This re.search() function searches the string/paragraph for a match and returns a match object if there is a match. The group() method is used to return the part of the string that is matched.

import re
string = 'TTTTTTPPPPPPPPPPPP'
match = re.search(r'T{4}P{3}', string)
print (match.group(0))

The following output is obtained on executing the above program -

TTTTPPP

Using findall() Function

In the following example, we match 'AAAAABB' string, which is present in the string 'AAAAAAAAABBBBB'. Here, we have used A{5}B{2} to match the required string.

Example

The following example is a program which shows how to specify repetitions Regex in Python using findall() function. We begin by importing the regular expression module.

Then, we have used findall() function, which is imported from the re module. The re.findall() function returns a list containing all matches.

import re
string = 'AAAAAAAAABBBBB'
res = re.findall(r'A{5}B{2}', string)
print (res)

The following output is obtained on executing the above program ?

['AAAAABB']
Updated on: 2025-04-24T13:21:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements