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?
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?
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.")
[%+-]
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-]
.