What does this regex mean in python? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

This question already has an answer here:

I am new to Python and I am trying to understand this regex:

pattern = r"^[A-Z0-9._%+-]+@[A-Z0-9.-]{2,200}$" 

What does %+- and .- mean?

回答1:

[%+-] means match either %, +, or -. Why we don't use escape character \ ? because they are in side []

[.-] means match either . or - Why we don't use escape character \ ? because they are in side [],

Furthermore - can also mean a range if between a range characters like [A-Z] or [0-9] in other case it is treated literally as in [AZ-].



回答2:

They're inside [], so they're part of a character class. It matches those literal characters.

A - is a special character inside a character class only if it appears somewhere in the middle of the class (as in A-Z, where it means the whole range of characters from A to Z). If it appears at the beginning or end of the class, it means a literal -.

(That regular expression looks like it matches an email address, for a certain definition of "email address.")



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!