How to write Python regular expression to get zero or more occurrences within the pattern?

In this article we will be using Regular Expression to get zero or more occurrences within the pattern. Regular expressions (regex) are important features to identify patterns in text. In Python, the re module provides support for working with regex. The concept of zero or more occurrences is a key regex feature that allows a pattern to appear anywhere from zero to infinite times.

Understanding Zero or More Occurrences

In regex, the * symbol denotes zero or more instances of the preceding element. For example, the pattern a* will match ?

  • An empty string (zero occurrences)

  • "a" (one occurrence)

  • "aa" (two occurrences)

  • "aaa" (three occurrences)

  • And so on...

Example 1: Matching Zero or More Letters

In this example, we define the regex pattern a*, which matches zero or more occurrences of the character 'a' in a string ?

import re

# Define a string to search
input_string = "aaa bbb cc d eee"

# Use regex pattern for zero or more 'a'
pattern = r'a*'

# Find all matches
matches = re.findall(pattern, input_string)
print("Matches found:", matches)

The output of the above code is ?

Matches found: ['aaa', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

Example 2: Matching Zero or More Whitespace Characters

In this example, we use \s* to match zero or more whitespace characters. This is useful for handling variable spacing in text ?

import re

# Define a string with various spaces
input_string = "Hello     World"

# Use regex to match zero or more whitespace
pattern = r'\s*'

# Find all matches
matches = re.findall(pattern, input_string)
print("Whitespace matches:", matches)

# More practical example - splitting by multiple spaces
words = re.split(r'\s+', input_string)
print("Words:", words)

The output of the above code is ?

Whitespace matches: ['', '', '', '', '', '     ', '', '', '', '', '', '']
Words: ['Hello', 'World']

Example 3: Validating Email Format

This example demonstrates email validation using zero or more occurrences. The pattern [a-zA-Z0-9]* accepts zero or more alphanumeric characters before the '@' symbol ?

import re

# Define sample email strings
emails = ["contact@example.com", "@example.com", "user123@domain.org"]

# Match email format with zero or more characters before @
pattern = r'^[a-zA-Z0-9]*@[a-zA-Z]+\.[a-zA-Z]+$'

for email in emails:
    if re.match(pattern, email):
        print(f"'{email}' matches the pattern.")
    else:
        print(f"'{email}' does not match the pattern.")

The output of the above code is ?

'contact@example.com' matches the pattern.
'@example.com' matches the pattern.
'user123@domain.org' matches the pattern.

Example 4: Extracting HTML Tags

This example shows how to extract HTML tags using zero or more occurrences. The regex <.*?> matches HTML tags with zero or more characters between angle brackets ?

import re

# Define a string containing HTML
html_content = "<div>Hello</div><span>World</span><br>"

# Regex to find HTML tags (non-greedy)
pattern = r'<.*?>'

# Find all HTML tags
tags = re.findall(pattern, html_content)
print("HTML tags found:", tags)

# Extract content between tags
content_pattern = r'>([^<]*)<'
content = re.findall(content_pattern, html_content)
print("Content between tags:", content)

The output of the above code is ?

HTML tags found: ['<div>', '</div>', '<span>', '</span>', '<br>']
Content between tags: ['Hello', 'World']

Common Use Cases

Pattern Description Example Match
a* Zero or more 'a' characters "", "a", "aa", "aaa"
\d* Zero or more digits "", "1", "123", "999"
\s* Zero or more whitespace "", " ", " ", "\t\n"
.* Zero or more any characters "", "abc", "123!@#"

Conclusion

The asterisk (*) quantifier in regex matches zero or more occurrences of the preceding pattern. Use it with character classes like [a-zA-Z]* or special sequences like \d* for flexible pattern matching in text processing tasks.

Updated on: 2026-03-24T19:03:00+05:30

992 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements