在本文中,我们将讨论re包的VERBOSE标志以及用户如何使用它。

re.VERBOSE

正则表达式包的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 : username12432@gmail.com
Output : Valid ID


Input : username14332@rediff.com@
Output : Invalid ID
This ID is invalid because there is @ character after the domain name.
Input : username32@.com 
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")   

输出:

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

 username123456721@redif.com@ : is Invalid Id
 username32@.com : is Invalid Id

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程