Verbose in Python Regex

Python Methods and Functions | Regular Expressions

re.VERBOSE : This flag allows you to write regular expressions that look better and more readable, allowing you to visually separate logical sections of the template and add comments. 
Spaces in the pattern are ignored unless they are in a character class, or when preceded by an unescaped backslash, or inside tokens such as * ?, (?: or (? P When a line contains a # character that is not a character class and is not preceded by a backslash without escaping, all characters from the leftmost such # to the end of the line are ignored.

# Without using VERBOSE

regex_email = re. compile (r ' ^ ([a- z0-9 _.-] +) @ ([0-9a-z .-] +). ([az.] {2, 6}) $ ' ,

re.IGNORECASE)

  
# Using VERBOSE

regex_email = re. compile (r "" "

^ ([a-z0-9 _ / .-] +) # local part

@ # single @ sign

([0-9a-z /.->] # Domain name

  /. # single dot

([az] {2,6}) $ # Top-level domain

"" " , re. VERBOSE | re.IGNORECASE) 

It is passed as an argument re.compile () i.e.  re.compile (Regular Expression, re.VERBOSE) re.compile () returns a RegexObject, which is then matched against the given string.

Let's look at an example where the user is asked to enter their email id, and we have to test it with RegEx. The email format is as follows:

  • Personal data / local part like john123
  • Single @
  • Domain name like gmail / yahoo etc. .
  • Single period (.)
  • Top-level domain, such as .com / .org / .net

Examples:

  Input : [email protected]  Output : Valid  Input : avadakedavra @ yahoo.com @  Output : Invalid This is invalid because there is @ after the top level domain name. 

Below is the Python implementation —

# Python3 program to show VERBOSE implementation in RegEX

import re

 

def validate_email (email):

 

  # RegexObject = re.compile (Regular expression, flag)

# Compiles the regular expression pattern to

  # regex object

  regex_email = re. compile (r "" "

^ ([a-z0-9 _ / .-] +) # local part

@ # single @ sign

  ([0-9a-z /.- Wavebreakmedia+) # Domain name

/. # single dot

([az] {2,6}) $ # Top-level domain

"" " , re. VERBOSE | re.IGNORECASE)

 

# RegexObject matches the desired

# line using the fullmatch function

# If a match is found, search ()

# returns a MatchObject instance

res = regex_email.fullmatch (email)

 

# If a match is found, the string is valid

if res:

  print ( " {} is Valid. Details are as follow: " . format (email))

 

# prints the first part / personal information about the email id

print ( "Local: {}" . format (res.group ( 1 )))

 

# prints Email ID domain name

print ( "Domain: {}" . format (res.group ( 2 ) ))

 

# prints Top-level domain name of email id

print ( "Top Level domain: {}" . format (res.group ( 3 )))

print ()

 

else :

# If the match is not Yes, the string is incorrect

print ( "{} is Invalid" . format (email))

 
Driver code

validate_email ( "[email protected]" )

validate_email ( "avadakedavra @ yahoo.com @" )

validate_email ( "Crucio @ .com" )

Exit:

 [email protected] is Valid. Details are as follow: Local: expectopatronum Domain: gmail Top Level domain: com avadakedavra @ yahoo.com @ is Invalid Crucio @ .com is Invalid 




Get Solution for free from DataCamp guru