Change language

Verbose in Python Regex

|

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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically