How to Find Matching any Single Char Using Python Re

Regular expressions
How to Find Matching any Single Char Using Python Re


Python regular expression helps you work with expressions. The short name for it is 're'. The major point to note here is Regular expression offers a parser that can be used to match strings within the text.

You can search the file name from the text you know.

The best example is We often use simple wildcards while searching for anything on our computer. The most commonly used character for conducting search is asterisk *. So, when you type '*.pdf' it would return all the pdfs available in the location where you are conducting your search.

How to use re?


You need to import 're', if you want to work with regular expressions.

>>> import re



How to use [...] to find any single char?


To match any single character you can use [….].The following example checks if there is a match with any of the letters: 'l' or 'a' or 'b'

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


The word 'love' has the letter 'l'. Hence, a match is found. The output is as below:

We found a match here @ l


Related posts

Comments

Most Popular

7 AWS Interview Questions asked in Infosys, TCS

How to Decode TLV Quickly

Hyperledger Fabric: 20 Real Interview Questions