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
Related
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.
I want to match the username before # in address mail,
and i create this regex
[A-Za-z+ /w+0-9._%+-]+#
the result of my example is:
example: blabla,blabla,Test#Testing.com,blabla,blabla,blabla
result : Test#
How can I get only Test without #.
The simplest way is:
([A-Za-z /0-9._%+-]+)#
and than use at what you taken ($1 in perl, match var in tcl, etc.)
btw,
I didn't know email addresses can have spaces in them, are you sure you're not taking too much in?
Edit:
here's a little tutorial on lookaheads (supporting Wiktor's comment)
http://www.regular-expressions.info/lookaround.html
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.
Is there a way to differentiate between an email address and a twitter id?
Both use the '#' character and the email regex will be contained by the twitter id regex.
What's the best way to approach this?
Should I require a whitespace before the '#' character in order to identify that it's a twitter id?
Not entirely sure which characters are allowed in twitter usernames, but basically like so:
/(?:^|\s)#[a-zA-Z0-9_.-]+\b/
You can test that it's preceded by whitespace using (?<=\s) and then check for the valid characters of twitter IDs which are only [A-Za-z0-9_].
That gives you a resulting regex of: (?<=\s|^)#[A-Za-z0-9_]+
You could eventually add a check for a dot, comma or whitespace after it to check that it's properly formatted within a sentence and not some weird artifact:
(?<=\s|^)#[A-Za-z0-9_]+(?=[\s.,])
Note that the lookbehind and lookahead (?<= and ?=) might not work in your language of choice, but I'll assume it does since you didn't specify.
Email addresses never start with an #, while twitter ids always do.
isTwitter = address[0] == '#'
A twitter id wouldn't pass an email regex check.
Regular email:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
twitter won't have the last characters:
^#[A-Za-z0-9_]+$
So check if it's a valid email, if not, check if it's a valid twitter ID
Farther reading:
How to Find or Validate an Email Address
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.