Python正则表达式中的详细说明

2021年3月17日15:19:55 发表评论 643 次浏览

在本文中, 我们将学习详细的标志重新包装以及如何使用它。

重新详细

:

该标志允许你可视化地分隔模式的逻辑部分并添加注释, 从而使你可以编写看起来更美观, 更易读的正则表达式

模式中的空格将被忽略, 除非是在字符类中, 或者以未转义的反斜杠开头, 或者在诸如

* ?, (?:或(?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(正则表达式, re.VERBOSE).re.compile()返回一个正则表达式对象然后与给定的字符串匹配。

让我们考虑一个示例, 在该示例中, 要求用户输入其电子邮件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

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: