Prevent multiple emails in one line in Google Forms using regex - regex

I have a long-form field ("Paragraph" type) in a Google Form. Users are expected to fill in any number of email addresses - at least one email, could be as many as 20-50 email addresses for some users.
I want to make sure that:
Each line is likely to be a valid email (by checking for a "#" character and a "." character)
Each line contains ONLY ONE email (by checking for "#" characters not separated by line breaks)
I know I can use the following string to check for two valid email addresses separated by a line break:
[a-zA-Z0-9_\.\+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+\n+[a-zA-Z0-9_\.\+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]
However, this limits the user to submitting two (no more, no less) email addresses.
Is there a way to check for 1 email address per line, and allow anything from 1 to multiple emil addresses?

You could write the pattern with anchors and repeating 1 or more newlines followed by the same pattern.
^[\w.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+(?:\n+[\w.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+)*$
See a regex101 demo

Related

Postfix Blocking E-mail from address containing two # symbols

My mail server has been receiving a lot of spam with malicious links and attachments lately which have "From" addresses that contain a valid address from my domain with an additional #someotherdomain.hn address appended.
For example:
John Smith has an e-mail address of JohnSmith#mydomain.com
I receive e-mail with a "from" address of JohnSmith#mydomain.com#someotherdomain.hn
I would like to know if it is possible to block e-mail if the "from" address contains more than one "#" symbol.
You tag as Regex so I assume you want a regex that matches addresses with 2 '#' symbols.
Here it is:
/#.*#/
This will match any address with 2 (or more) #-signs.
It simply starts with matching '#', followed by zero or more characters, and finally another '#'.
All you have to do is delete the mail, if there's a match.
Edit:
To only match when one mail address has two '#' signs, and not when there are two addresses (each with a single #), you can use this Regex (assuming addresses are separated by comma ','):
/#[^,]*#/
Again, if there's a match, delete it.

Regex Multiple email validation

I Want to implement multiple email validation with specific domain using regex separated by ;. Requirement is to allow only two email id separated by semicolon.
I tried regex
^([\w+-.%]+#domain+\.com[~]?){1,2}$
it works fine but it validates email followed by ; . It should work as below
1) abc#domain.com valid
2) abc#domain.com;abc1#domain.com valid
3) abc#domain.com; invalid
4) abc#domain.com;abc1#domain.com; invalid
How to do it using regex?
Try:
"^([\w+.%-]+?#domain\.com)(;([\w+.%-]+?#domain\.com))?$"
It consists of addres 1
([\w+.%-]+?#domain\.com)
and zero or one further addres
(;([\w+.%-]+?#domain\.com))?
"^...$" makes shure that there are no other characters around.
Group 1 and Group 3 will contain the addresses
Test here

Ignore dots and allow + in e-mail addresses in mailgun (regex)

I'm new to mailgun and I'm trying to create a filter for "Matching on variations of the recipient of the email". I want it to work just like gmail. Example:
jondoe#gmail.com, jon.doe#gmail.com and j.o.n.d.o.e#gmail.com are the same accounts. Basically I want to remove dots from addresses.
I also want to add option + at the end of address for forwarding purposes. Example: jonedoe+facebook#gmail.com.
This is a regex for adding + at the end but how do I remove the dots?
match_recipient("^chris\+(.*)#example.com$")
Actually I have this now:
(chris)(#example.com)|(^((.*\.)+.*)#example.com$)|(^(chris)\+(.*)#example.com$)
And this will match this variations:
chris#example.com
chris+#example.com
chris+d#example.com
chris+3#example.com
chris+d8#example.com
chri.s#example.com
c.hri.s#example.com
c.h.r.i.s+#example.com
c.h.r.i.s+2d#example.com
.chris#example.com
chris.#example.com
And won't match these (as it shouldn't):
ch5ris#example.com
cchris#example.com
chriu#example.com
chriOs#example.com
chbis#example.com
cchhrriiss#example.com
michael#example.com
The only issue now is that it matches names with dots at the beginning and at the end. How do I fix this now?

Regex for matching a mix of delimited tokens and email address

I need to validate a string for valid email addresses or specific tokens (later to be replaced by email addresses) or a mix of both delimited by semi-colon. I need a little help with this regex I nearly got working.
It matches the tokens but not the email address at the start or end.
^(((<#a#>)+|[;])*|(([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*|((<#a#>)+|[;])*)$
Here is the answer posted by AlexBay which is working with my test data.
^((<#a#>)+|[;]|(([a-zA-Z0-9_\-\.]+)#([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})‌​+)+$

How to detect that a certain string is not an email address but a twitter id?

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