0

Regex Expression for finding a string which has one @ and should not start with . or end with . Meaning hello@Same is valid .hello@Same, hello@Same. and hello@sdj@same are all invalid

What is the problem in (^([^@]+)@([^@])+$)(^[^\\.].*$)(^.*[^\\.]$).

All these three parts work individually but when we put together doesn't work

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • A good comment on the subject : http://stackoverflow.com/a/1076589/966590 – Andrew Logvinov Feb 24 '12 at 07:59
  • Why would you not allow multiple @s or no starting dot? Both may be valid in email adresses. – Jens Feb 24 '12 at 08:21
  • Basically ... What i want to know is can i say an expression should match (patternA) && (patternB) && (patternC) and each of the pattern can be evaluated individually on expression .. each pattern should have its own start of line symbol and end of line symbol ... In the above example ... patternA evaluates only @ and patternB to doesn't start with . and then patternC to doesn't end with . .All work individually well how to combine all three at a place ? – user1230217 Feb 24 '12 at 08:48
  • And expression having the meaning patternA && patternB is `(?=patternA)patternB` – Jens Feb 24 '12 at 10:14

3 Answers3

1

You have multiple start of line symbols ^ and multiple end of line symbols $ in your regexp.

Dervall
  • 5,736
  • 3
  • 25
  • 48
1
^([^.@]+)@([^.@]+)$

To break it down to more manageable pieces:

^([^.@]+)  // Start with anything except . or @
@          // @ must be somewhere in the expression
([^.@]+)$  // End with anything except . or @

It doesn't match your specifications exactly (can't start or end with @) but that is probably a desirable attribute if you're validating email addresses.

  • This expressions allows multiple @ signs (e.g. @boo@boo@). Also, why are you using "[^@]|\w+" (either a single non-@ character, or one or more alphanumerics)? Thats not part of the OPs specs. =) – Jens Feb 24 '12 at 08:31
  • @Jens - Fixed the original problem but now there's another (currently doesn't allow for periods). I'll look at this again tomorrow morning. I'm beginning to see what you mean about this being a bad way to validate email addresses. 8) –  Feb 24 '12 at 08:44
0

You can't just concatenate regular expressions if you want to match all of them.

Another thing that is wrong is trying to validate email addresses with a regular expression. This just does not work well. The only way to find out if the user entered a valid adress is to send an email.

A regular expression that matches your specifications would be

^(?=[^@]+@[^@]+$)[^.].*[^.]$

but, as I said, thats not a great way to validate email adresses. Check this link for horrible counter examples. =)

Jens
  • 25,229
  • 9
  • 75
  • 117