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 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.
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:
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.
[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