0

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?

1

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)@

2 Comments

I think your [\w.] is too restrictive, something like \S (a non whitespace character) would be probably better.
Will this guarantee they will be at the start of the emails address? For instance 'dadmin' or 'emspam' would not be matched? Thanks for your help folks! I'm a bit new to this.
0

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)@.+

2 Comments

Will this guarantee they will be at the start of the emails address? For instance 'dadmin' or 'emspam' would not be matched? Thanks for your help folks! I'm a bit new to this
to make sure they are at the start of the email you could use the ^ character at the start if you are just trying to match emails. However if you are trying to search for emails in a long string of text then use \s instead right before. ^ says this has to be the start of the string but \s says this has to be whitespace before the email starts. you can check it out here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.