How to Search for Single CHAR in Python Using Regular-expression

Here is the logic for searching single CHAR using regular expression(Regex). For instance, we use wildcards to search for anything on our computers. The Regex in Python works similarly.

Regular Expression

Regular expression

People use asterisk * for searching any document. For instance, if you type *.pdf, it returns all the pdfs available in the location (where you are conducting your search). Similar way, in Python, you can search using regular expressions.

Import Regex 

The first thing you need to do is import 're' if you want to work with regular expressions.

Example program: search for single CHAR

To match any single character, you can use [….]. Below, you will find an example to search for: 'l' or 'a' or 'b'

import re
pattern = r'[lab]' sequence = 'we love python' obj =,sequence) if obj: print("We found a match here @", else: print("Sorry no match found.")

The word 'love' has the letter 'l'. Hence, a match is found.

Post a Comment

Thanks for your message. We will get back you.

Previous Post Next Post