-3

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?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
archura
  • 461
  • 1
  • 5
  • 9
  • 4
    They are just matching those characters / symbols . – MooingRawr Sep 11 '18 at 20:50
  • 1
    `[%+-]` match either `% ` `+ ` or `-` – The Scientific Method Sep 11 '18 at 21:06
  • 1
    `%` and `+` have no special meaning inside `[ ]`, so they are just characters to be matched. `-` does have special mean, defining a range of characters, as you see wit e.g. `A-Z`, but if it appears first `[-xxx]` or last `[xxx-]`, then it is treated as just a character to match. The same could be done with escaping `[xx\-xx]`. – Andreas Sep 11 '18 at 21:35

2 Answers2

4

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.")

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
2

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

The Scientific Method
  • 2,151
  • 2
  • 10
  • 21