Python正则表达式中的VERBOSE标志
在本文中,我们将讨论re包中的VERBOSE标志以及用户如何使用它。
re.VERBOSE
regex包的VERBOSE标志允许用户编写更美观且更易读的正则表达式。通过使用该标志,用户可以在模式的逻辑部分之间添加可视分隔,并添加更多注释。
模式中的空格会被忽略,但当空格存在于字符类中、或者在未转义的反斜杠之前、或者在诸如* ? , ( ? P或(? :, 空格无法被忽略的符号中时,空格将不会被忽略。然而,只要#在行中存在,并且在字符类中不存在或者在未转义的反斜杠之前,从#到行尾的所有字符都将被忽略。
示例
import re as regex
def validating_email(emails):
# First, we will see without Using VERBOSE
reg_emails = regex.compile(r'^([z-a1-3_\.-0]+)@([0-1a-s\.-]+)\.([c-z\.]{5, 8})',
re.IGNORECASE)
# Using VERBOSE
reg_email = regex.compile(r"""
^([z-a1-3_\.-0]+) # local Part like username
@ # single @ character
([0-1a-s\.-]+) # Domain name
\. # single Dot .
([c-z\.]{5, 8}) # in the end, the Domain
""",re.VERBOSE | re.IGNORECASE)
上述命令作为参数传递给 re.compile() 方法,该方法写作”re.compile(正则表达式, re.VERBOSE)”。re.compile()方法将返回正则表达式对象,该对象将与给定的字符串进行匹配。
使用Verbose标志的用例
让我们通过一个示例来更好地理解。假设要求用户输入其电子邮件地址,并且开发人员必须使用正则表达式对其进行验证。电子邮件的格式如下:
- 用户的个人详细信息/本地部分(如用户名:Mark3213)
- 单个“@”字符
- 域名,如Gmail、Hotmail、Fastmail、inbox、jubii、Rediff等
- 单个点(.)
- 最后,域名如.in、.net、.org、.com、.int、.edu等
Input : stash
Output : Valid ID
Input : stash@
Output : Invalid ID
This ID is invalid because there is @ character after the domain name.
Input : stash
Output : Invalid Id
This ID is invalid because there is no domain is the Email ID.
示例1:
# Python program for showing the Implementation of VERBOSE flag in Python RegEX
import re as regex
def validating_email(emails):
# RegexObject = re.compile( Regular expression , flag )
# This will compile the regular expression pattern into the regular expression object.
reg_email = regex.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 will be matched with the desired string by using the fullmatch() function.
# If the match of the email is found, search() function will return the MatchObject instantly.
ress = reg_email.fullmatch(emails)
#If the email match is found, the string is valid to use
if ress:
print (" {} : is Valid email. Details of it are as follow: ".format(emails))
# now print the first part that is personal detail of Email Id user
print (" Local : {}".format(ress.group(1)))
# now, print the Domain Name of validated Email Id
print (" Domain : {}".format(ress.group(2)))
# now, print the Domain Name of the validated Email Id
print (" The domain name : {}".format(ress.group(3)))
print ()
else:
# and, If the match is not found, the string is an inval id
print (" {} : is Invalid Id".format(emails))
# the Driver Code :
validating_email ("username12432@gmail.com")
validating_email ("username123456721@redif.com@")
validating_email ("username32@.com")
输出:
stash : is Valid email. Details of it are as follow:
Local : username12432
Domain : gmail
The domain name : com
username12345672stash@ : is Invalid Id
stash : is Invalid Id