JasonDavis
JasonDavis

Reputation: 48933

Can someone explain this regex

I know this is a pretty basic regex, could someone explain what it is doing please?

^[^@]+@[-a-z0-9.]+$

Upvotes: 0

Views: 185

Answers (5)

Simon Nickerson
Simon Nickerson

Reputation: 43159

I think it's trying to match an email address (not very well)

Example matches:

Upvotes: 5

Austin Salonen
Austin Salonen

Reputation: 50215

To expand upon Rex's answer, it looks like a naive email validation regex.

Upvotes: 0

moonshadow
moonshadow

Reputation: 89055

^ - match start of string

[^@]+ - match one or more characters that aren't an @

@ - match an @

[-a-z0-9.]+ - match one or more characters from the set '-', lower case 'a'-'z', the digits '0'-'9', '.'

$ - match end of string

So, match any string that consists of some characters that aren't '@', followed by '@', followed by some number of lower case letters / digits / dashes / full stops.

Upvotes: 9

John Rasch
John Rasch

Reputation: 63435

Matches a string that doesn't start with at least 1 @ character, followed by matching a @, then a -, . or any alphanumeric characters at least once.

I'm guessing it's a very loose email validator.

Upvotes: 1

Rex M
Rex M

Reputation: 144112

It says "match one or more non-@ character followed by an @, followed by one or more alphanumeric characters, a - or a ." The ^ at the beginning and the $ at the end signify this pattern must also be against the beginning and end of the entire string (^ means "beginning of string" and $ means "end of string").

Upvotes: 2

Related Questions