Python Programs for Most commonly used Regex – 2022

Machine Learning Projects

So guys in this blog we will see the Python Programs for most commonly used regex like regex to check mobile phones, regex to check email, and many more. So without any further due, let’s do it…

Most commonly used Regex codes

Regular ExpressionsDescription
foo.*# Matches any string starting with foo
\d*# Match any number of decimal digits
[a-zA-Z]+# Match a sequence of one or more letters
textMatch literal text
.Match any character except newline
^Match the start of a string
$Match the end of a string
*Match 0 or more repetitions
+Match 1 or more repetitions
?Match 0 or 1 repetition
+?Match 1 or more, as few as possible
*?Match 0 or more, as few as possible
{m,n}Match m to n repetitions
{m,n}?Match m to n repetitions, few as possible
[…]Match a set of characters
[^…]Match characters, not in a set
A | BMatch A or B (…) Match regex in parenthesis as a group
\numberMatches text matched by the previous group
\AMatches start of the string
\bMatches empty string at beginning or end of the word
\BMatches empty string not at begin or end of the word
\dMatches any decimal digit
\DMatches any non-digit
\sMatches any whitespace
\SMatches any non-whitespace
\wMatches any alphanumeric character
\WMatches characters not in
\w \ZMatch at end of the string.
\\Literal backslash

What is the regex to check a 10-digit mobile phone?

# Python Programs for Most commonly used Regex

import re

regex_expression = "[6-9][0-9]{9}"

mobile = input('Enter a mobile number - ')

if len(mobile)==10:
    m = re.fullmatch(regex_expression ,mobile)
    if m!=None:
        print('Valid 10 digit number')
    else:
        print('Not a valid 10 digit number')
else:
    print('Please enter a 10 digit mobile number')
  • The regex says that the number should start from either 6,7,8 or 9.
  • And then after that, there should be 9 numbers from the range (0,9).

Output

Enter a mobile number – 9999999999
Valid 10 digit number

Enter a mobile number – 1999999999
Not a valid 10 digit number

Enter a mobile number – 99999
Not a valid 10 digit number

What is the regex to check valid email?

# Python Programs for Most commonly used Regex

import re

regex_expression = "[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}"

email = input('Enter an email id - ')

e = re.fullmatch(regex_expression ,email)

if e!=None:
    print('Valid email')
else:
    print('Not a valid email')

Output

Enter an email id – asharma@gmail.com
Valid email

Enter an email id – ..@gmail.com
Not a valid email

Enter an email id – asharma123@gmail.com
Valid email

What is the regex to check the date?

# Python Programs for Most commonly used Regex

import re

regex_expression = "\d{2}/\d{2}/\d{4}"

date = input('Enter a date - ')

e = re.fullmatch(regex_expression ,date)

if e!=None:
    print('Valid date')
else:
    print('Not a valid date')
  • The regular expression says that the string should match nn/nn/nnnn where n is a number.
  • \d is used to match decimals.

Output

Enter a date – 02/02/2022
Valid date

Enter a date – 02/2/2022
Not a valid date

Enter a date – 2/2/2022
Not a valid date

Do let me know if there’s any query while using the Most commonly used Regex in Python by contacting me via email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time …

Read my previous post: How to get the current date and time in Python

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projects, and Flask projects at machinelearningprojects.net

Leave a Comment

Your email address will not be published.