Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Isn't this [A-Za-z] character class redundant here?

I have this regex, ^[A-Za-z][0-9A-Za-z\.\@\-]{7,59}$, but it seems to me that the first character class [A-Za-z] is completely redundant. Is that correct?

Upvotes: 2

Views: 106

Answers (2)

Sebas
Sebas

Reputation: 21532

no, because ^[A-Za-z] means it has to begin with a letter (caps or not). Without it, it could begin with a letter or a number or anything matching [0-9A-Za-z\.\@\-]

Upvotes: 3

El Ronnoco
El Ronnoco

Reputation: 11922

No because that is insisting that the string starts with alpha (and ASCII alpha at that)

^ is the start-anchor, so this string must start with alpha - then you may have any of the chars specified by the second character class.

And if you are using this regex to validate a string (some kind of variable name?) then you may also need the end-anchor $. Otherwise it will not care what follows the match...

^[A-Za-z][0-9A-Za-z\.\@\-]{7,59}$

This means that the string must be no more than 60 characters in total (including the leading alpha).

Upvotes: 11

Related Questions