Chris
Chris

Reputation: 111

Regex - Matching a string within an email address

So my problem is I am looking to match a certain combination of letters at the start of an email address followed by an @ and then a wildcard, for example:

admin@* OR noreply@* OR spam@* OR subscribe@

Can this be done?

Upvotes: 0

Views: 243

Answers (3)

Neil Essy
Neil Essy

Reputation: 3607

Your looking for grouping with the | operator. The following will do what you want.

edit: Since your using this for an email server rules you won't need to match the entire string, only part of it. In that case you will need to use ^ to specify the start of the string and then drop the domain portion since we don't care about what it is.

^(admin|noreply|spam|subscribe)@

Upvotes: 1

stema
stema

Reputation: 92976

Try this

^(?:admin|noreply|spam|subscribe)@\S*

See it here on Regexr

You need an anchor at the start to avoid matching address with other characters before. If the string contains only the email address use ^ at the beginning, this matches the start of the string. If the email address is surrounded by other text, the use \b this is a word boundary.

(?:admin|noreply|spam|subscribe) is a non capturing group, becuase of the ?: at the start, then there is a list of alternatives, divided by the | character.

\S* is any amount of non white characters, this will match addresses that are not valid, but should not hurt too much.

Upvotes: 1

hackartist
hackartist

Reputation: 5264

sure thing!

[A-Za-z]+@.+

says any letters at least once but any number of times, then an at sign, then anything (other than newline) for your specific examples use

(admin|noreply|spam|subscribe)@.+

Upvotes: 0

Related Questions