Python正则表达式中的冗长
在本文中,我们将了解re 包的VERBOSE标志以及如何使用它。
re.VERBOSE
:此标志允许您通过允许您在视觉上分离模式的逻辑部分并添加注释来编写看起来更好且更具可读性的正则表达式。
模式中的空格被忽略,除非在字符类中,或者前面有未转义的反斜杠,或者在像*?, (?: or (?P
) 这样的标记内。当行包含不在字符类中的 # 时并且前面没有未转义的反斜杠,则从最左边的 # 到行尾的所有字符都将被忽略。
# 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)
它作为参数传递给re.compile()
即re.compile(Regular Expression, re.VERBOSE) 。 re.compile()
返回一个RegexObject ,然后与给定的字符串匹配。
让我们考虑一个示例,要求用户输入他们的电子邮件 ID,我们必须使用 RegEx 对其进行验证。电子邮件的格式如下:
- 个人详细信息/本地部分,如 john123
- 单身的 @
- 域名如 gmail/yahoo 等
- 单点 (.)
- .com/.org/.net 等顶级域
例子:
Input : expectopatronum@gmail.com
Output : Valid
Input : avadakedavra@yahoo.com@
Output : Invalid
This is invalid because there is @ after the top level domain name.
下面是Python的实现——
# 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("expectopatronum@gmail.com")
validate_email("avadakedavra@yahoo.com@")
validate_email("Crucio@.com")
输出:
expectopatronum@gmail.com is Valid. Details are as follow:
Local:expectopatronum
Domain:gmail
Top Level domain:com
avadakedavra@yahoo.com@ is Invalid
Crucio@.com is Invalid