My issue is finding a Regex that matches the following rules.
The main Regex I'm using to check if the email is valid is /^\w ([.-]?\w )*@\w ([.-]?\w )*(\.\w{2,3}) $/
I would like to use 3 additional Regexs to match:
- characters AND @ (nothing after @)
- @ domain OR @ domain top-level-domain
- characters AND NO @
These 3 Regexs should be sufficient to check if:
- A domain hasn't been entered
- A username hasn't been entered
- If the @ sign hasn't been entered
If the input doesn't match these rules then they will receive a generic error message.
CodePudding user response:
Using Regex101.com I was able to create my own regular expressions and use their explanations to understand them. I am using 8 Regexs for this email input:
/^\w ([.-]?\w )*@$/;
/^\w ([.-]?\w )*$/;
/^\w ([.-]?\w )*(\.) $/;
/^@\w ([.-]?\w )*$/;
/^@\w ([.-]?\w )*(\.)$/;
/^@\w ([.-]?\w )*(\.\w{2,3}) $/;
/^\w ([.-]?\w )*(\.\w{2,3}) $/;
/^\w ([.-]?\w )*@\w ([.-]?\w )*(\.\w{2,3}) $/;
Using these expressions and one test against whether the input is empty, I display the following messages:
- Enter your email address
- Don't forget to include '@'.
- Enter a domain after '@'.
- Enter a username before the '@'.
- This email address is not valid.
CodePudding user response:
This is a valid email address using reg expression:
[\w\d._] @[\w\d] \.[\w]{2,3}
All what you need to do is to create a condition statement against the above string. If it matches, then it's a valid email address, otherwise it's a non valid address.
Below is a Python example (which you can still modify to match whatever language you want):
import re
pattern = "^[a-zA-Z0-9] @[a-zA-Z0-9] \.[a-zA-Z]{2,3}$"
user_input = input()
if(re.search(pattern, user_input)):
print("This is a valid email")
else:
print("This isn't a valid email")