Difference Between & And in Python Regular Expression



This article will discuss the difference between dot '.', question mark '?', and asterisk'*' in Python regular expressions.

In regular expressions, '.' means any one character except the newline, '*' means zero or more times of the character or group before it. and '?' means zero or one time of the character or group before it. In some types of regex,? Also makes things non-greedy.

As we have seen in Python regular expressions, these symbols have different meanings, so let us understand these symbols using the examples -

Usage of Dot (.)

The following example will use the dot '.' to match any single character except a newline.

# Import re module
import re

# define text or string here
txt = "cat cot cut"

# Match with the pattern
pat = r"c.t"

# Use findall method
res = re.findall(pat, txt)

# print the result
print(res)  

Output

This will create the following outcome -

['cat', 'cot', 'cut']

Usage of Question Mark (?)

This is another example to show the usage of the question mark '?' in Python regular expressions. So basically, ? makes the preceding character optional; it can be 0 or 1 occurrence.

# Import re module
import re

# define text or string here
txt = "color colour"

# Match with the pattern
pat = r"colou?r"

# Use findall method
res = re.findall(pat, txt)

# print the result
print(res) 

Output

This will generate the following result -

['color', 'colour']

Usage of Asterisk (*)

In this example, we are showing the usage of the asterisk '*', which is used to match zero or more occurrences of the previous character.

# Import re module
import re

# define text or string here
txt = "ab aabb aaabbb"

# Match with the pattern
pat = r"a*b"

# Use findall method
res = re.findall(pat, txt)

# print the result
print(res) 

Output

This will produce the following result -

['ab', 'aab', 'b', 'aaab', 'b', 'b']

Difference Between ., ? and *

Here are the key differences between these symbols -

  • . (dot) matches any single character (excluding newlines).

  • * is a quantifier that denotes zero or more of the previous regex atom (character, group, etc.).

  • ? is a quantifier that denotes zero or one of the preceding atoms. In some regex variants, ? can be used as a non-greedy (lazy) modifier following quantifiers (for example, *?, +?).

Updated on: 2025-06-06T14:40:28+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements