4

I have this regex for email validation (assume only x@y.com, abc@defghi.org, something@anotherhting.edu are valid)

/^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$/i

But @abc.edu and abc@xyz.eduorg are both valid as to the regex above. Can anyone explain why that is?

My approach:

  1. there should be at least one character or number before @

  2. then there comes @

  3. there should be at least one character or number after @ and before .
  4. the string should end with either edu, com, or org.
Dustin Sun
  • 5,292
  • 9
  • 49
  • 87
  • 2
    Not an answer, but relevant: http://davidcel.is/posts/stop-validating-email-addresses-with-regex/ – Will Nov 03 '15 at 16:10
  • What about all the people with email addresses like `wearewatchingyou@us.gov` and people from other countries like `bigleader@uk.co` or people who have email addresses like `first.last@something.com`. It's clear you have no idea on what constitutes a valid email address, so I suggest you use someone else's regex. – Robert McKee Nov 03 '15 at 16:11
  • Thanks but I am practicing regex now. I need to know why I am wrong. – Dustin Sun Nov 03 '15 at 16:11
  • https://regex101.com/r/yN1kJ7/1 – DTSCode Nov 03 '15 at 16:12
  • @Robert Mckee thank you for your reply it is just a regex practice. – Dustin Sun Nov 03 '15 at 16:12
  • If you use a website such as [Regex101](https://regex101.com/) you can view a complete explanation of your pattern and what it's doing. – SierraOscar Nov 03 '15 at 16:24

5 Answers5

3

Try this

/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.(com|edu|org)$/i

and it should become clear - you need to group those alternatives, otherwise you can match any string that has 'edu' in it, or any string that ends with org. To put it another way, your version matches any of these patterns

  • ^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)
  • (edu)
  • (org)$

It's worth pointing out that the original poster is using this as a regex learning exercise. This would be a terrible regex for actual production use! It's a thorny problem - see Using a regular expression to validate an email address for a lot more depth.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
3

Your grouping parentheses are incorrect:

/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.(com|edu|org)$/i

Can also just use one case as you're using the i modifier:

    /^[a-z0-9]+@[a-z0-9]+\.(com|edu|org)$/i

Regular expression visualization

N.B. you were also missing a + from the second set, I assume this was just a typo...

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
3

What you have written is the equivalent of matching something that:

Begins with [a-zA-Z0-9]+@[a-zA-Z0-9].com

contains edu

or ends with org

What you were looking for was:

/^[a-z0-9]+@[a-z0-9]+\.(com|edu|org)$/i
Robert McKee
  • 21,305
  • 1
  • 43
  • 57
2

Your regex looks ok.

I guess you are looking using a find function in stead of a match function

Without specifying what you use it is a bit difficult, but in Python you would write

import re
pattern = re.compile ('^[a-zA-Z0-9]+@[a-zA-Z0-9]\.(com)|(edu)|(org)$')
re.match('@abc.edu') # fails, use this to validate an input
re.search('@abc.edu') # matches, finds the edu
Ward
  • 2,802
  • 1
  • 23
  • 38
  • 1
    Not the easiest answer to follow, but what you're saying is essentially correct in that we assume the code is finding sub-match and therefore returning true. On that basis I'll give you an upvote as this is technically what the OP has asked – SierraOscar Nov 03 '15 at 16:28
-2

Try to use it: [a-zA-Z0-9]+@[a-zA-Z0-9]+.(com|edu|org)+$

U forget about + modificator if u want to catch any combinations of (com|edu|org)

Upd: as i see second [a-zA-Z0-9] u missed + too

Ruslan Galimov
  • 256
  • 3
  • 10
  • This doesn't answer the question. – ghoti Nov 03 '15 at 16:17
  • Sry, but why? http://pythex.org/?regex=[a-zA-Z0-9]%2B%40[a-zA-Z0-9]%2B\.%28com|edu|org%29%2B&test_string=a%40aacomcomcomcom&ignorecase=0&multiline=0&dotall=0&verbose=0 – Ruslan Galimov Nov 03 '15 at 16:30
  • @RuslanGalimov, you are failing to terminate. http://pythex.org/?regex=%5Ba-zA-Z0-9%5D%2B%40%5Ba-zA-Z0-9%5D%2B%5C.(com%7Cedu%7Corg)%2B&test_string=a%40example.comsomeotherstring&ignorecase=0&multiline=0&dotall=0&verbose=0 – ghoti Nov 03 '15 at 16:51
  • Your updated regex also matches the string `1@blorgorg`. You might want to try testing your regex with things you know should fail, in addition to ones that you know should work. – ghoti Nov 04 '15 at 13:31