In this article, we will learn about
VERBOSE flag of the
re package and how to use it.
re.VERBOSE
: This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments.
Whitespace within the pattern is ignored, except when in a character class, or when preceded by an unescaped backslash, or within tokens like
*?, (?: or (?P
. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.
Python3
# Without Using VERBOSE
regex_email = re.compile(r'^([a-z0-9_\.-]+)@([0-9a-z\.-]+)\.([a-z\.]{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 .
([a-z]{2,6})$ # Top level Domain
""",re.VERBOSE | re.IGNORECASE)
It's passed as an argument to
re.compile()
i.e
re.compile(Regular Expression, re.VERBOSE).
re.compile()
returns a
RegexObject which is then matched with the given string.
Let's consider an example where the user is asked to enter their Email ID and we have to validate it using RegEx. The format of an email is as follow:
- Personal details/local part like john123
- Single @
- Domain Name like gmail/yahoo etc
- Single Dot(.)
- Top Level Domain like .com/.org/.net
Examples:
Input : [email protected]
Output : Valid
Input : [email protected]@
Output : Invalid
This is invalid because there is @ after the top level domain name.
Below is the Python implementation -
Python3
# Python3 program to show the Implementation of VERBOSE in RegEX
import re
def validate_email(email):
# RegexObject = re.compile( Regular expression, flag )
# Compiles a regular expression pattern into
# a regular expression object
regex_email=re.compile(r"""
^([a-z0-9_\.-]+) # local Part
@ # single @ sign
([0-9a-z\.-]+) # Domain name
\. # single Dot .
([a-z]{2,6})$ # Top level Domain
""",re.VERBOSE | re.IGNORECASE)
# RegexObject is matched with the desired
# string using fullmatch function
# In case a match is found, search()
# returns a MatchObject Instance
res=regex_email.fullmatch(email)
#If match is found, the string is valid
if res:
print("{} is Valid. Details are as follow:".format(email))
#prints first part/personal detail of Email Id
print("Local:{}".format(res.group(1)))
#prints Domain Name of Email Id
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 match is not found,string is invalid
print("{} is Invalid".format(email))
# Driver Code
validate_email("[email protected]")
validate_email("[email protected]@")
validate_email("[email protected]")
Output:
[email protected] is Valid. Details are as follow:
Local:expectopatronum
Domain:gmail
Top Level domain:com
[email protected]@ is Invalid
[email protected] is Invalid
Similar Reads
re.search() in Python re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example:Pythonimport re s = "Hello, welcome to the worl
3 min read
re.subn() in Python re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a
3 min read
Python RegEx Regular Expression (RegEx) is a powerful tool used to search, match, validate, extract or modify text based on specific patterns. In Python, the built-in re module provides support for using RegEx. It allows you to define patterns using special characters like \d for digits, ^ for the beginning of a
8 min read
Regex Cheat Sheet - Python Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern.
9 min read
Python - Regex split() re.split() method in Python is generally used to split a string by a specified pattern. Its working is similar to the standard split() function but adds more functionality. Letâs start with a simple example of re.split() method:Pythonimport re s = "Geeks,for,Geeks" # Using re.split() to split the st
3 min read
Python NLTK | tokenize.regexp() With the help of NLTK tokenize.regexp() module, we are able to extract the tokens from string by using regular expression with RegexpTokenizer() method. Syntax : tokenize.RegexpTokenizer() Return : Return array of tokens using regular expression Example #1 : In this example we are using RegexpTokeni
1 min read