'Regex expression (Exclude)
I am working on a regex function that verifies if the email address is valid. All seem to work but this email address "[email protected]" should return 'false' in the condition because it contains underscore. The following is the code that I have written. Is there a way I could exclude underscore(_) from my search?
import re
def emailCheck(email):
result = re.search(r'((A-Za-z0-9.[_])*)@((\d?[a-z]?[A-Z]?)*(\b.com\b|\b.edu\b|\b.org\b))', email)
return bool(result)
Solution 1:[1]
Your issue is (A-Za-z0-9.[_])*. This is just a optional listing of arbitrary characters A, Z, a, z, 0, 9, -, and _.
You need a character class for ranges. It should be [A-Za-z0-9.]+ and add a start/end line meta escape.
^([A-Za-z0-9.]+)@((\d?[a-z]?[A-Z]?)*(\b.com\b|\b.edu\b|\b.org\b))$
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MonkeyZeus |
