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

4
  • A good comment on the subject : stackoverflow.com/a/1076589/966590 Commented Feb 24, 2012 at 7:59
  • Why would you not allow multiple @s or no starting dot? Both may be valid in email adresses. Commented Feb 24, 2012 at 8: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 ? Commented Feb 24, 2012 at 8:48
  • And expression having the meaning patternA && patternB is (?=patternA)patternB Commented Feb 24, 2012 at 10:14

3 Answers 3

1

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

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

Comments

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.

2 Comments

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 - 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)
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. =)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.