Regexp to find hostname and subdomain from email address [duplicate] - regex

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse email addresses for “from” and “to” fields in Ruby
Given an email address of the form root#host.sub.domain.com, what is the regular expression to get the hostname and the subdomain as two individual variables? I mean $1='host'and $2='sub'?
You help is greatly appreciated :)
EDIT I should say I'm asking about a regular expression to do this, no Ruby code. This is to be used in a postfix configuration file, so I need a regexp. I mistakenly talked about Ruby earlier. My fault, sorry for the confusion.

Non-greedy groups are your friend. They match just what you need:
^(?:.*?)#(.*?)\.(.*?)\.(?:.*?)\.(?:.*?)$

Related

Regex: matching domain.com and any subdomains to domain.com? [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 2 years ago.
Hi folks I've been at this for 2 hours and figured there has to be someone out there with a better solution?
In this regex case, I need to match all domains and subdomains from bbc. Eg:
bbc.com
subdomain.bbc.co.uk
But not sites such as
another-bbc.com
anotherbbc.com
bbcmore.com
I need to keep the "bbc" part flexible so I can use other domains in future such as facebook.
The closest Ive got to is (.*\.)?bbc\..* but it's matching anotherbbc.
Please can you help me? Would appreciate this.
The following one should suit your needs:
^([^.\r\n]+\.)*(bbc\.com|bbc\.co\.uk)$
Allows any amount of subdomains for the hardcoded domains.
For this specific case, you could replace (bbc\.com|bbc\.co\.uk) with bbc\.(com|co\.uk), but if you need other domains as well (e.g. like you said, facebook.com), I believe it will be more maintainable to hardcode them entirely.
See https://regex101.com/r/PnMYpY/1.

Regex - disallow www in email address [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 5 years ago.
I am using a regular expression to validate email addresses in an online form. A lot of emails are entered as such: www.test#example.com
How can I disallow the use of www. using regex? My current expression:
^[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$
I know this expression checks for a gTLD - we require an email address to have one.
EDIT: This question is slightly different to others I have found as I wasn't sure how to add the www exclusion in - that's all I needed.
Also, the target market we reach doesn't have the same experience using technology as most people do, so the amount of email addresses that we receive with www in them is massive. www.john#gmail.com is perfectly valid yes but it is 99% of the time not what the persons email address is. Our CRM system can't send emails to the correct people then.
you can use the regex
^(?!www\.)[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$
see the regex demo

Regular expression for complicated email [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I want an regex for email that starts with numbers and letters.
My regex is
/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
What is wrong in this? It allows ._- from start and in between. But I don't want this
Valid emails = s#gmail.com, s.p#y.com, s_p123#g.com
Invalid emails = ....s#g.com, s---g#g.com, s...#g.com, 44s..p#g.com, ----s#g.com
Does anyone know how this can be done?
So you dont want to have more than one ._- in your regex? And also it shall not start with ._- Try it this way:
^[a-zA-Z0-9]+[._-]?[a-zA-Z0-9]*#[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}
It works for your examples, although im not really sure what email-syntax you really want to test for.
Perhaps you want to use one of many pre written examples in the web (just google ;) )

REGEX email matching [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have a JAVA string that is being used to validate proper email addresses.
^[\\w'-]+(\\.[\\w'-]+)*#[A-Za-z0-9]+([.-][A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
I want to user to be able to leave the address blank and not get an error message. How is this possible?
Since you didn't provide any other code or specify what language you were using, the best I can suggest is:
(?:^[\\w'-]+(\\.[\\w'-]+)*#[A-Za-z0-9]+([.-][A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$)|(?:^$)
Which will match an e-mail or an empty string.
If you have an expression you want to make optional allowing empty string to match you could use any of the following:
^(?:regex)?\z
^\z|^regex\z

how to get strong email validation REGEX [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using a regular expression to validate an email address
I am using email address validation in my code and for that I am using a Regex.
Can anyone suggests me a regex which will not accept email addresses eg. abc#def.commmmmm or abc#xyz.com.com.com or abc#gmail.c.
Please help me.
thanks
any information is valuable for me.
REGEXP =
/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
i have used in validation engine as well as custom js file.