REGEX to match domain in email addresses - regex

I'm currently using this very relaxed REGEX to validate email addresses:
(/(.+)#(.+)\.(.+){2,}/.test(emailAddress)
I believe that it only allows min1_allowsDots#min1_allowsDots.min2 - is this correct?
How should I modify it to match only a particular domain - anything# onlythisdomain.com?
TIA!
iSofia

(.+)#onlythisdomain\.(.+){2,}
This should do it.If .com is also fixed use
(.+)#onlythisdomain\.com

This simple RegEx basically says:
At least one character, then an "#", then literally "gmail.com".
If you change the domain to something else, it will not match.
var email = 'Godisgood#gmail.com';
var re = /.+\#gmail\.com/;
alert(email.match(re));

Maybe this could help you:
^[a-zA-Z0-9_.+-]+#(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?
(company_domain1|company_domain2)\.ca$/g
It is a regex where you can validate the email including a specific domain such as
jhosh#doctorgroup.ca or jhosh#doctors.ca and it will match only if the email is ending with #doctorgroup.ca or #doctors.ca.

Related

regex extract username from 2 types of url

I'm currently using this regex (?<=\/movie\/)[^\/]+, but it only matches the username from the second url, i know i could make a if (contains /movie/): use this regex, else: use another regex on my code, but i'm trying to do this directly on regex.
http://example.com:80/username/token/30000
http://example.com:80/movie/username/token/30000.mp4
To complete the Tensibai's answer, if you have not a port in url, you can use the last dot in url to start your regex :
\.[^\/\.]+\/(?:movie\/)?([^\/]+)
(demo)
You can use something like this to make the movie/ optional and have the username in a named capture group (Live exemple):
\d[/](?:movie\/)?(?<username>[^/]+)[/]
using \d/ to anchor the start of match at after the url.

Limit domain part of email using regex

I have a regex for email validation, what I wanna do is limit the characters for the domain part, I know that I will use this {1,10} but I don't know where should I put it. Thank you
/(?!.*\.{2})^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0]))*(([ \t]*\r\n)?[ \t]+)?")#(([a-z\d\u00A0]|[a-z\d\u00A0][a-z\d\-._~\u00A0]*[a-z\d\u00A0])\.)+([a-z\u00A0]|[a-z\u00A0][a-z\d\-._~\u00A0]*[a-z\u00A0])\.?$/i
Add (?![^#]{11}) after ^ to disallow 11 characters other than # at the start of string:
(?!.*\.{2})^(?![^#]{11})([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0]))*(([ \t]*\r\n)?[ \t]+)?")#(([a-z\d\u00A0]|[a-z\d\u00A0][a-z\d\-._~\u00A0]*[a-z\d\u00A0])\.)+([a-z\u00A0]|[a-z\u00A0][a-z\d\-._~\u00A0]*[a-z\u00A0])\.?$
See proof

Specfic Email Address Validation using Regex

I have a registration form where email address is needed.
I need to allow the email addresses that belongs only to domain("NAME"#Mobily.com.sa) this is for employees that working for "MOBILY"
Also we need to disallow the emails that is ( "NAME".c#mobily.com.sa ) where those emails that ends up with (.c) belongs to the contractors working for "Mobily".
The Idea is to allow the employees and disallow the contractors from registration.
Please Advise the proper REGEX to achieve this.
Use this:
var regEx = /^(\w+)#(mobily\.com\.sa)$/i;
In case you want to support name as "abc.def", do
var regEx = /^(\w+\.?\w+)#(mobily\.com\.sa)$/i;
Examples:
regEx.test("ab212121c#mobily.com.sa") //true
regEx2.test("ab212121.c#mobily.com.sa") //false
The following regex can have a everything as name except names ending with .c
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+(?!\.c).{2}#mobily\.com\.sa$/i
See here
https://regex101.com/r/6vgQbj/2
You can use a negative lookahead to prevent .c from occurring right before the #.
/[\w\.\+-]+?(?!\.c).{2}#mobily\.com\.sa/g
See the regex in action here:
https://regex101.com/r/i0Rg34/2
please use this:
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#mobily.com.sa

fetch email address that does not contain certain words from webpage

Please tell me how can I extract emails (1st match) that does not contain words 'ajax' and 'gif' within email address. Here's the basic regex that I have:
match = re.findall('([\w.-]+#[\w.-]+\.\w+)', q.read(), re.I)[0]
Thanks,
I think you want something like this,
\b(?!\S*(?:ajax|gif)\S*)([\w.-]+#[\w.-]+\.\w+)\b
DEMO

Regex empty string or email

I found a lot of Regex email validation in SO but I did not find any that will accept an empty string. Is this possible through Regex only? Accepting either empty string or email only? I want to have this on Regex only.
This regex pattern will match an empty string:
^$
And this will match (crudely) an email or an empty string:
(^$|^.*#.*\..*$)
matching empty string or email
(^$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)
matching empty string or email but also matching any amount of whitespace
(^\s*$|^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)
see more about the email matching regex itself:
http://www.regular-expressions.info/email.html
The answers above work ($ for empty), but I just tried this and it also works to just leave empty like so:
/\A(INTENSE_EMAIL_REGEX|)\z/i
Same thing in reverse order
/\A(|INTENSE_EMAIL_REGEX)\z/i
this will solve, it will accept empty string or exact an email id
"^$|^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$"
I prefer /^\s+$|^$/gi to match empty and empty spaces.
console.log(" ".match(/^\s+$|^$/gi));
console.log("".match(/^\s+$|^$/gi));
If you need to cover any length of empty spaces then you may want to use following regex:
"^\s*$"
If you are using it within rails - activerecord validation you can set
allow_blank: true
As:
validates :email, allow_blank: true, format: { with: EMAIL_REGEX }
Don't match an email with a regex. It's extremely ugly and long and complicated and your regex parser probably can't handle it anyway. Try to find a library routine for matching them. If you only want to solve the practical problem of matching an email address (that is, if you want wrong code that happens to (usually) work), use the regular-expressions.info link someone else submitted.
As for the empty string, ^$ is mentioned by multiple people and will work fine.