Multiple Email validation in a single input field separated by ; - regex

Currently i am writing a software where a user can input more than one email in a input field separated by: ";"
Now i have a regex that validates the email but sadly enough doesn't work when i have more Emails in the input field when using the separation.
Has anyone ever created such a regex or is there anyone that is able to help me?
Thanx in advance and looking forward for a response.
Here is my Regex:
[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(\;|)

Just put the pattern which matches the following emails inside a non-capturing group with a preceding ; and make it to repeat zero or more times.
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(?:;[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+)*$
And one more thing is, you need to escape the dot.

Related

Flutter email input formatter does not work

I have a Flutter TextFormField for email with input formatter as below.
var emailAddressFormatter = FilteringTextInputFormatter.allow(RegExp(
r"[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+"));
The problem is, when trying to input any character in the field it does not allow. The regex looks fine to me. When the formatter is removed the field accepts any character with any format. What am I going wrong?
The issue is that the FilteringTextInputFormatter that you're using rejects anything that does not match your regex. When you enter just a single character, it does not match your regex, so the character is rejected.
I know little about regex so I'm not sure if it's possible, but you would need a regex that would be able to match every string as you type e.g. a, am, amani#, amani#gmail.com.
I would personally not try to do filtering such as this. Instead, I would just allow all valid characters that are valid in email addresses to be present in the email and not enforce the specific format with the # and .. Then I would use a validator to check that the email is valid upon form submission.
If you don't like the alternate solution I proposed above and you can't use regex, you can make your own input formatter quite easily with TextInputFormatter.withFunction.

How to write regex for domain validation that will trigger pattern error in Angular form

I am validating a domain field in the form. I am using Validators.pattern(this.domainPattern) for doing that.
I am using below pattern:
public domainPattern: string = "^(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$";
It works fine for many cases. But when there is a white space in domain it is not triggering pattern error. What I am missing?
Quick help will be highly appreciated.
Thanks.
Try this pattern:
(?(?<= )(?=[^ ])|^)(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]
I just added (?(?<= )(?=[^ ])|^), conditional which checks:
first it checks condition (?<= ) if what is preceeding is space, if it is, then check if what's after is not a space with (?=[^ ]), if the condition fails, then check if we are at the beginning of a string with ^.
Demo
UPDATE
OP said:
I want user to enter just one valid domain name. If user enters "google.com google.com" it should be treated as invalid
Then you could use this pattern
^(?!.* .*)(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$
Just added (?!.* .*) which checks if there's sapce in following line, if it is, then it won't match anything, as space indicated multiple domain names.
Another demo

Get an exact regex match of an email value from a list of email addresses

I have a text field which stores a list of email addresses e.g: x#demo.com; a.x#demo.com. I have another text field which stores the exact value matched from the list of emails i.e. if /x#demo.com/i is in x#demo.com;a.x#demo.com then it should return x#demo.com.
The issue I am having is that if I have /a.x#demo.com/i, I will get x#demo.com instead of a.x#demo.com
I know of the regex expression /^x#demo.com$/i, but this means I can only have one email in my list of email addresses which won't help.
I have tried a couple of other regex expressions with no luck.
Any ideas on how I can achieve this?
You can use this slightly changed regex:
/(^|;)x#demo.com($|;)/i
It will match from either beginning of string or start after a semi colon and end either at end of string or at a semi colon.
Edit:
Small change, this uses look behind and look forward, then you will only get the match, you want:
(?<=^|;)x#demo.com(?=$|;)
Edit2:
To allow Spaces around the semi colon and at start and end, use this (#-quoted):
#"(?<=^\s*|;\s*)x#demo.com(?=\s*$|\s*;)"
or use double escaping:
"(?<=^\\s*|;\\s*)x#demo.com(?=\\s*$|\\s*;)"

regex match multiple capture groups in any order

Given the sample string below I'm trying to capture the 'to', 'from', 'subject' and 'type' fields and spit them back out in a different format. The issue is that these fields (to, from, etc) can be in any order.
SAMPLE STRING TO REGEX ON
<cfmail to="#toAddr#" from="#fromAddress"
subject="#subject#" type="html">
#emailMsg#
</cfmail>
OUTPUT I'M LOOKING FOR
to:toAddr, from:fromAddress, subject:subject
If I knew that the order of those field I'm interested in was always the same then this is pretty easy, but I'm stumped on how to do this matching if, for instance, 'from' comes before 'to'
The perl one-liner I have right now is (just testing with 'to' and 'subject')
s/<cfmail.*?((to)="(.*?)")|((subject)="(.*?)").*<\/cfmail>/\1:\2, \3:\4/g
This ends up matching the 'to' value but stops there and I don't get anything for the 'subject' value. I've tried several variations on this where I change matching group setup etc but have had no luck on it.
Do you need to allow for missing fields (e.g. no type field)? What about other fields in addition to those four? If you answered no to both questions, this regex should do the trick:
s!<cfmail(?:\s+to="(?<to>[^"]+)"|\s+from="(?<from>[^"]+)"|\s+subject="(?<subject>[^"]+)"|\s+type="(?<type>[^"]+)")+>.*?</cfmail>!to:$+{to}, from:$+{from}, subject:$+{subject}!gs
Here's the regex alone in more readable form:
<cfmail
(?:
\s+to="(?<to>[^"]+)"
|
\s+from="(?<from>[^"]+)"
|
\s+subject="(?<subject>[^"]+)"
|
\s+type="(?<type>[^"]+)"
)+
>
.*?</cfmail>
...and a DEMO
You were actually pretty close; alternation was the key. You just needed to add a quantifier.
Notice that I removed the capturing groups from the field names. You already know the names, you just need to pair them with the correct values. The named groups make that much easier.

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.