Verbose Flag in Python Regex

Verbose Flag in Python Regex

In this article, we will discuss about the VERBOSE flag of the re package and how the users can use it.

The re.VERBOSE

The VERBOSE flag of the regex package allows the user to write regular expressions that can look nicer and are more readable. This flag does that by allowing the users to visually separate the logical sections of the pattern and add more comments.

The whitespaces inside the pattern are ignored, but when the whitespace is present in the character class or when it is preceded by the unescaped backslash, or when it is inside the tokens such as * ? , ( ? P or (? :, whitespaces cannot be ignored. Although, whenever # is present in the line, which is not in the character class or is not preceded by the unescaped backslash. All the characters from the leftmost of # to the end of the line will be ignored.

Example –

The above commands are passed as an argument to the re.compile() method, which is written as “re.compile(Regular Expression , re.VERBOSE). The re.compile() method will return the REGEX OBJECT, which will be matched with the given string.

Use case of Verbose Flag

Let’s see an example to understand it in a better way. Suppose the users are asked to enter their EMAIL ID, and the developer has to validate it by using RegEX. The format of the EMAIL is written below:

  • Personal details of the users/ local part like username: Mark3213
  • Single @ character
  • Domain names such as Gmail, Hotmail, Fastmail, inbox, jubii, Rediff, and many more.
  • Single Dot (.)
  • In the end, Domains like .in, .net, .org, .com, .int, .edu, and many more.
Input : [email protected]
Output : Valid ID


Input : [email protected]@
Output : Invalid ID
This ID is invalid because there is @ character after the domain name.
Input : [email protected] 
Output : Invalid Id
This ID is invalid because there is no domain is the Email ID.

Example 1:

Output:

[email protected] : is Valid email. Details of it are as follow: 
 Local : username12432
 Domain : gmail
 The domain name : com

 [email protected]@ : is Invalid Id
 [email protected] : is Invalid Id

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263499.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论