3

I'm validating mail addresses that could look something like this:

foo@bar.xx.com

but also

foo@bar.yy.com

I would like to check whether if xx is present or not. The first thing that came to mind was String.Contains, but it would obviously match any occurrences of xx in the string.

Is regexp the way to go? If so, please help me with the pattern.

Update: The mail can have any ending, e.g. .com, .ru etc.

filur
  • 2,116
  • 6
  • 24
  • 47

3 Answers3

2

You can use this regex to test.

It will ensure that after the @ there is .xx. but may also match the string @.xx.*

.*@[^.]*[.]xx[.]

Or this one to ensure that there is at least one character before and after the @.

.+@[^.]+[.]xx[.]
Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12
  • Thanks. In what way does this differ from the other answer? – filur Jun 22 '15 at 18:23
  • This pattern is also match by this string: `@.xx.` !! and it's not correct! – Behzad Jun 22 '15 at 18:33
  • The poster asked to be able to validate mail addresses containing `.xx.` after the @, ignoring the end of the address. In what way does this not meet that requirement? – Jeffrey Wieder Jun 22 '15 at 18:35
  • @JeffreyWieder `.xx.` just a condition but i think that pattern must be valid email address. – Behzad Jun 22 '15 at 18:40
  • That is not what the poster asked for. "I would like to check whether if xx is present or not." This does THAT. If the poster would like a regex that validates it is a valid email AND has `.xx.` i will update my answer to accommodate that. @filur which were you looking for? – Jeffrey Wieder Jun 22 '15 at 18:54
  • First of all, I'm not the downvoter. To be more specific, I want to check if the mail looks like this `*@*.xx.*` where `*` is just a wildcard. Does it make sense_ – filur Jun 22 '15 at 18:56
  • Thank you for the clarification, i have updated my answer with another option if you want at least one character before and after the `@` character. – Jeffrey Wieder Jun 22 '15 at 19:00
2

There is no simple way to assure that the email is valid using regex, check it here for more details.

However for your needs and a basic verification you can use the following regex:

^[^@]+@[^@]+\.(xx|XX).[^@^.]+$
Community
  • 1
  • 1
A B
  • 497
  • 2
  • 9
0

You can use this pattern: \w+@\w+.xx.\w+

Check Reference: http://regexr.com/

And if you want to check any email, better to seen this link patterns.

Community
  • 1
  • 1
Behzad
  • 3,502
  • 4
  • 36
  • 63