Learn Regular Expression applied on Python Programming Language RegEx

Learn how to write Regular Expression Syntax in every programming Language such as String Pattern matching, Meta Characters, user input validation etc
Learn Regular Expression applied on every scripting  and programming Language - www.pdfcup.com

The Regular Expression symbols and syntax are same for every programming language there is just a minor difference of different programming language syntax where regular expression is implementing.

Regular Expression Symbols Combinations (RegEx Rules):

RegEx Pattern Discription
[abc] Any Character only from a,b,c
[^abc] Any other Character except a,b,c
[a-z] Any lower Character from 'a' to 'z'
[A-Z] Any upper/Capital Character from 'A' to 'Z'
[a-z A-Z] Accept any combination of lower and upper character.
[0-9] Accept only numeric digits '0' to '9'
$ Find ending of the string
^ Find beginnig of the string
| OR [Note: only work in complex expressions]
() Split the long Regular expression in sub-groups
. Match any single character.

Regular Expression Quantifiers Symbols Combinations:

RegEx Pattern Discription Remark
[rule]? Possibility of expression match is either zero ('0') or one ('1') times
[rule]+ Possibility of expression match atleast one ('1') or Multiple ('1',...) times
[rule]* Possibility of expression match is either Zero ('0') or Multiple ('1',...) times
[rule]{n} Possibility of expression occure 'n' times. i.e: if n==10: then it will occure 10 times.
[rule]{n,} Possibility of expression match atleast 'n' times or Multiple ('n',...) times.
i.e, if n=10 then it will atleast occure 10 times or more than 10 times.
[rule]{y,z} Possibility of expression match atleast 'y' times but less than 'z' times.
i.e, if y=10 and z=20, than it will atleast accure 10 times but less than 20 times.

Regular Expression Meta Character (Short method of writing RegEx rules):

RegEx Meta Char Equivalent To Description
\d [0-9] Representing only numbers are allowed
\D [^0-9] Representing only numbers are not allowed
\w [a-z A-Z 0-9] Representing any alphabets and numbers are allowed
\W [^\w] Representing none of alphabets and numbers are allowed

Write the Regular expression to remove the digits and special character pattern from the string followed by below instructions.

    Instructions
  • Find the count of special characters and digits in each word
  • Remove the special characters and digits from the words
  • Multiply each word with the count of special characters in that word
  • Reverse the order of words
  • Return the new string

Example 1: Write a pattern to find the list of Mobile Numbers Starts with either 7 or 9 and ends with 0.

Solution: pattern = [79][0-9]{9}

Example 2: Write a pattern to check the user provided inputs contains First Upper Letter follow by Lower Letters and ended with single number.

Solution: pattern = [A-Z][a-z]+[0-9]

Example 3: Write a pattern to check the user provided email id is vaild or not.

Solution: pattern = [a-z A-Z 0-9_\-\.]+[@][a-z A-Z]+[\.][a-z A-Z]{2,7}

Explanation:

Used RegEx Pattern Purpose of symbols
[a-z A-Z 0-9_\-\.] This expression allows to enter any 'username' including Lower-Upper alphabets, digits, hyphen, underscore combinations only, i.e. "myfacebook_Blog".
[@] It is representing that 'at-the-rate' symbol comes only once just after entering the username.
[a-z A-Z] This block allowing to enter the 'domain' name either in small or upper letters, i.e. "gmail".
[\.] This block indicating that 'Dot' symbol comes only once just after entering the domain name.
[a-z A-Z]{2,7} This block allowing to enter the 'extension' name either in small or upper letters which has minimum character range is two and maximum is 7, i.e. "com,in,us,uk,tech, etc.".

Example 4: Write a regular expression parttern to check the given word is either 'a' or 'b', or combination of 'a' and 'b', i.e. {a, b , abb, bbbaa, etc.}

Solution: pattern = [a+b]*

Example 5: Write a regular expression parttern to check the given string is starts with 'a' but not having consective b's, i.e. {a, aab, abaa, etc.}

Solution: pattern = [a+ab]*

Example 6: Write a regular expression parttern to check the given string is zero "0" and has only even sequence, i.e. {00, 0000, 000000 etc.}

Solution: pattern = [00]*

Example 7: convert user inputed 12 hours time format into 24 hours time format using Regular expression.

Solution

import re
def timeConversion(s):
    f = re.findall("\d+|\w+", s)
    
    hour= int(f[0])
    mi = f[1]
    sec = f[2]
    amPM = f[-1]
    
    if amPM =='PM' and hour!=12:
        hour = 12+ hour
    elif hour==12 and amPM =='AM':
        hour= 0
    else:
        hour = hour
            
    result = f'{hour:02d}:{mi}:{sec}'
    return result

#Function Call
timeConversion("07:05:45PM")  

#Output: 19:05:45

Example 8: Write a code using regex to match a specific word from the given string.

Solution


import re

#Test_String = input()
Test_String = "Learn Regular Expression applied on every scripting  and programming Language such as Java, Rust, Ruby, and Python."

# Task: Find how many times the word "and" appear in the given string?

Regex_Pattern = r'\band\b'
match = re.findall(Regex_Pattern, Test_String)

print("Number of matches :", len(match))

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment