---
title: "6 Essential Python Regular Expression Examples"
description: "Here are the powerful Python regular expression examples and popular methods to read right away"
url: https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/
date: 2023-04-24
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/04/python-regular-expression-examples.jpg?fit=2240%2C1260&ssl=1
categories: ["Python"]
tags: ["Examples", "Regular expression"]
type: post
lang: en
---

# 6 Essential Python Regular Expression Examples

In Python, regular expressions play a vital role in dealing with datasets. These metacharacters are critical in use in regular expressions. Here’re the top examples.

List of metacharacters

*Python Regex *

```
. ^ $ * + ? { } [ ] \ | ( )
```

The meaning of the preceding metacharacters is here:

- `?` (matches 0 or 1): the expression `a?` matches the string `a` (but not `ab`)
- `*` (matches 0 or more): the expression `a*` matches the string `aaa` (but not `baa`)
- `+` (matches 1 or more): the expression a+ matches aaa (but not baa)
- `^` (beginning of line): the expression `^[a]` matches the string `abc` (but not `bc`)
- `$` (end of line): `[c]$` matches the string `abc` (but not `cab`)
- `.` (a single dot): matches any character (except newline)

**ON THIS PAGE**

1. [Python regular expression examples](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#python-regular-expression-examples)
   1. [#1 Matching a specific string pattern:](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#1-matching-a-specific-string-pattern)
   2. [#2 Matching any character in a set:](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#2-matching-any-character-in-a-set)
   3. [#3 Matching one or more occurrences of a character:](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#3-matching-one-or-more-occurrences-of-a-character)
   4. [#4 Matching zero or more occurrences of a character:](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#4-matching-zero-or-more-occurrences-of-a-character)
   5. [#5 Matching a specific number of occurrences of a character:](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#5-matching-a-specific-number-of-occurrences-of-a-character)
   6. [#6 Removing meta characters from a string](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/#6-removing-meta-characters-from-a-string)

## **Python regular expression examples**

You’ll find the real examples on python regular expression.

- Matching a specific pattern
- Matching any Char
- Matching one of more occurrence of Char
- Matching zero or more occurrence of Char
- Matching specific number of occurrences

### #1 Matching a specific string pattern:

The search() methods searches for a pattern.

```
import re
pattern = "apple"
text = "I like apples and oranges"
match = re.search(pattern, text)
if match:
    print("Match found!")
else:
    print("Match not found!")
```

The output

```
Match found!
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### #2 Matching any character in a set:

The findall() method will find any matching character.

```
import re
pattern = "[aeiou]"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
```

The output

```
['e', 'o', 'o', 'a', 'e', 'o', 'u']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

> Python CI/CD Pipeline [how to](https://srinimf.com/2023/08/07/python-mini-project-how-to-validate-email/) deploy the code – [A step-by-step guide](https://semaphoreci.com/blog/python-continuous-integration-continuous-delivery) to practice it on your laptop.

### #3 Matching one or more occurrences of a character:

```
import re
pattern = "o+"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
```

The output

```
['o', 'o', 'o']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### #4 Matching zero or more occurrences of a character:

```
import re
pattern = "o*"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
```

The output

```
['', '', '', '', 'o', '', '', '', 'o', '', '', '', '', '', '', '', 'o', '', '', '']
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### #5 Matching a specific number of occurrences of a character:

```
import re
pattern = "o{2}"
text = "Hello, how are you?"
matches = re.findall(pattern, text)
print(matches)
```

The output

```
[]
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

### #6 Removing meta characters from a string

The sub() method removes meta characters from the string.

```
import re
text1 = "meta characters ? and / and + and ."
text2 = re.sub("[/\.*?=+]+","",text1)
print('text1:',text1)
print('text2:',text2)
```

The output

```
text1: meta characters ? and / and + and .
text2: meta characters  and  and  and 
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Summary

- These are a few examples that you can do with regular expressions in Python.
- Regular expressions are a powerful tool for working with text, searching for specific patterns, validating input, etc.

References

- [Mastering Python regular expressions](https://www.amazon.in/Mastering-Python-Regular-Expressions-Felix/dp/1783283157?tag=srinimf-21)
- [Python regular expressions a pocket reference book](https://www.amazon.in/Regular-Expression-Pocket-Reference-OReilly/dp/0596514271/?tag=srinimf-21)
- [The Python Examples from Google Tutorial](https://developers.google.com/edu/python/regular-expressions#:~:text=%5Cw%20%2D%2D%20%28lowercase%20w%29,matches%20any%20non%2Dword%20character.)
- [Python Regular Expressions-NTU university Singapore](https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html#zz-1.9)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F04%2F24%2F6-essential-python-regular-expression-examples%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F04%2Fpython-regular-expression-examples.jpg%3Ffit%3D1200%252C675%26ssl%3D1&description=6%20Essential%20Python%20Regular%20Expression%20Examples)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/04/24/6-essential-python-regular-expression-examples/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%206%20Essential%20Python%20Regular%20Expression%20Examples&body=https%3A%2F%2Fsrinimf.com%2F2023%2F04%2F24%2F6-essential-python-regular-expression-examples%2F&share=email)
-

### Like this:

Like Loading…
