How to accept a dot within email address with Regex? - regex

I have this regex [a-z0-9]*#metu\.edu which checks user input userside, using HTML5. I want to accept a dot (.) within the username, only a single dot. Such as: herp.derp#metu.edu

use below
^[a-z0-9]+[.]?[a-z0-9]+#metu\.edu$
DEMO

[a-z0-9][a-z0-9.]*#metu\.edu\.tr
This will require at least one (lower case character or number) and then (lower case character or number or dot), so email addresses cannot begin with or only consist of a single dot. However you should probably reconsider using this regex for email validations, as the acceptable email addresses contain a lot more cases (-, +, _ etc.) (and don't forget size limitations as well)

In general, e-mail addresses shouldn't be matched with regexes. However, in your specific case, it seems that you have a distinct pattern that you want to match against.
[a-z0-9]+(\.[a-z0-9]+)?#metu\.edu
Assuming that the single dot is optional, if it's mandatory, use this:
[a-z0-9]+\.[a-z0-9]+#metu\.edu

Add a dot to the character range: [a-z0-9.]*
To exclude dots at the beginning or end of the name, and only allow a single dot use multiple character classes:
[a-z0-9]+\.?[a-z0-9]+#metu\.edu

You should not be trying to parse e-mail adresses yourself, using regex, as you WILL fail. Please consider this: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
What I usually do is accept any string as e-mail. If it's just for user registration or whatever, there is really no need to validate it. It will fail when you send out an email, then you know it's wrong.

Related

Regex of email addresses without having a separator

I am trying to extract email addresses that are in a text list that have no separator that could be used the detect beginning and ending of the addresses. I have a string like this:
email1#hotmail.comwelcome#mydomain.atinfo#another-domain.detesting#domain.or.atmy.name_test#domainname.de
I am on the following state of the regex (not working):
[a-zA-Z0-9.-]+#[a-zA-Z0-9-.]+.(com|at|de|or.at)
Would be very interesting if someone have a solution for this? Maybe also a better way to determine the domain ending than having a hardcoded list of all possibilities.
You're going to need that list of hardcoded TLDs, otherwise there's no way of determining where one address ends and where the next one begins.
Your regex is not bad, but you need to escape the . (otherwise it will match any character if not enclosed in a character class) and to allow underscores within your character classes:
[a-zA-Z0-9._-]+#[a-zA-Z0-9_.-]+\.(com|at|de|or\.at)
works for your examples.

Regex email address list parser doesn't pick up last item in list

Here is the regex101 demo.
I want to parse a list of email addresses separated by a variety of delimiters. The regex I am using is:
/(\S+?#\S+?\.\S+?)[,|;|\|\s|\n|\r|\t|\0|\b|$]/gmi
The problem is, in the example demo above, it doesn't pick up the last item in the list. How do I pick up the last email address in the list?
You can't use $ as a line/string terminator inside a character class, it will be understood as the literal dollar character : while /(\S+?#\S+?\.\S+?)[,|;|\|\s|\n|\r|\t|\0|\b|$]/gmi doesn't work, /(\S+?#\S+?\.\S+?)([,|;|\|\s|\n|\r|\t|\0|\b|]|$)/gmi does.
Additionally, I would suggest a number of improvements to your regex :
remove the pipes from the character class, unless you want to match a literal pipe
remove the NUL (\0) character from the character class. Not only should it never appear in your string, even if it did it would be matched by $
remove the linefeeds from your character class and/or stop using the m flag, unless a single address can be split in multiple lines
stop using the i flag, which won't affect the character classes you're using
I also doubt you want to match centralreservation#ramaya;nahotel.com as a valid address.
In conclusion, I suggest you use [^\s;,#]+#[^\s;,#]+\.[^\s;,#]+ instead, or better stop trying to validate email addresses with regex and instead use a specialized library. To understand why, check the regex this perl module uses to validate emails. And it doesn't even fully implement the RFC...
A big thanks to Sebastian Proske for his assistance.

Custom email validation regex pattern not working properly

So I've got /.+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]\#[\w+-?]+(.{1})\w{2,}/ pattern I want to use for email validation on client-side, which doesn't work as expected.
I know that my pattern is simple and doesn't cover every standard possibility, but it's part of my regex training.
Local part of address should be valid only when it has at least one digit [0-9] or letter [a-zA-Z] and can be mixed with comma or plus sign or underscore (or all at once) and then # sign, then domain part, but no IP address literals, only domain names with at least one letter or digit, followed by one dot and at least two letters or two digits.
In test string form it doesn't validate a#b.com and does validate baz_bar.test+private#e-mail-testing-service..com, which is wrong - it should be vice versa - validate a#b.com and not validate baz_bar.test+private#e-mail-testing-service..com
What specific error I've got there and where?
I can't locate this, sorry..
You need to change your regex
From: .+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]\#[\w+-?]+(\.{1})\w{2,}
To: .+[^\x20-\x2A\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]?\#[\w+-]+(\.{1})\w{2,}
Notice that I added a ? before the # sign and removed the ? from the first "group" after the # sign. Adding that ? will make your regex to know that hole "group" is not mandatory.
See it working here: https://regex101.com/r/iX5zB5/2
You're requiring the local part (before #) to be at least two characters with the .+ followed by the character class [^...]. It's looking for any character followed by another character not in the list of exclusions you specify. That explains why "a#b.com" doesn't match.
The second problem is partly caused by the character class range +-? which includes the . character. I think you wanted [-\w+?]+. (Do you really want question marks?) And then later I think you wanted to look for a literal . character but it really ends up matching the first character that didn't match the previous block.
Between the regex provided and the explanatory text I'm not sure what rules you intend to implement though. And since this is an exercise it's probably better to just give hints anyway.
You will also want to use the ^ and $ anchors to makes sure the entire string matches.

Regular Expression to not allow disposable email addresses

I'm trying to create a regex that does not allow disposable email addresses but allows everything else. So far, here is what I have:
^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(((?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9]))(?=.*(?!.*mailinator.com))(?=.*(?!.*trbvm.com))(?=.*(?!.*guerrillamail.com))(?=.*(?!.*guerrillamailblock.com))(?=.*(?!.*sharklasers.com))(?=.*(?!.*guerrillamail.net))(?=.*(?!.*guerrillamail.org))(?=.*(?!.*guerrillamail.biz))(?=.*(?!.*spam4.me|grr.la))(?=.*(?!.*guerrillamail.de))(?=.*(?!.*grandmasmail.com))(?=.*(?!.*zetmail.com))(?=.*(?!.*vomoto.com))(?=.*(?!.*abyssmail.com))(?=.*(?!.*anappthat.com))(?=.*(?!.*eelmail.com))(?=.*(?!.*yopmail.com))(?=.*(?!.*fakeinbox.com)))$
Right now, it accepts all email addresses.
Try this slightly modified regex using lookbehind:
^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(((?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9]))(?<!mailinator\.com)(?<!trbvm\.com)(?<!guerrillamail\.com)(?<!guerrillamailblock\.com)(?<!sharklasers\.com)(?<!guerrillamail\.net)(?<!guerrillamail\.org)(?<!guerrillamail\.biz)(?<!spam4\.me)(?<!grr\.la)(?<!guerrillamail\.de)(?<!grandmasmail\.com)(?<!zetmail\.com)(?<!vomoto\.com)(?<!abyssmail\.com)(?<!anappthat\.com)(?<!eelmail\.com)(?<!yopmail\.com)(?<!fakeinbox\.com))$
It matches bob#gmail.com but does not match bob#mailinator.com.
Fundamentally, you had a regex to match any email address, followed by positive and negative lookaheads like (?=.*(?!.*mailinator.com)). By the time those lookaheads are executed, you're already at the end of the string (further enforced by the $).
Looking ahead from the end of the string there is… nothing. Any lookahead (positive or negative) into nothingness will either always pass, or always fail, regardless of the input string. E.g. a lookahead of (?=.*) at the end of a string will always pass (.* matches the empty string), whereas one of (?=.) will always fail (. does not match the empty string).
In your case, the lookaheads like (?=.*(?!.*mailinator.com)) are okay with the nothingness beyond the end of the input string, so always pass. It's identical to if you didn't have them in the regex at all.
The simple fix, without overhauling the regex entirely, is to look behind with the (?<!) construct, instead of ahead. You're at the end of the string, and want to ensure it didn't end with one of the disposable email domains you have listed. To do that for one domain, it would be (?<!mailinator\.com).
There are many disposable email domains and they are constantly changing. Writing a regex for them is only going to capture a small number and will require constant maintenance and updating.
You may want to look at using some open source lists eg. https://github.com/disposable/disposable and then build a way to update them.
Alternatively you can use something like Upollo's free tier which does this for you.

Regex needed for password

I require a regex for password field
I tried before posting but couldn't get through.
which validates the field for:
at least one special char
at least one alphabetic character
at least one numeric char?
Here is an article on how to write regex password validation strings:
http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/
In your case, you would look for something like this:
^.*(?=.{8,})(?=.*\d)(?=.*[A-Za-z])(?=.*[.,!-##$%^&+=_]).*$
This would require your password to be at least eight characters, contain a letter, a number, and a special character (one of these: .,!-##$%^&+=_)
If you are really struggling with regex, I would suggest you try something like this (free) tool for helping you build regex expressions:
http://www.radsoftware.com.au/regexdesigner/
Here is a regex that will require at least one alpha, one numeric, and one special character.
^.*(?=.*[a-z])(?=.*[A-Z])(?=.*[\W])(?=.*[\d]).*$
More info here.