I need a Perl-compatible regular expression for filtering my email with smtp-gated. I only want to allow one domain ('mydomain.com') and reject everything else. How do I do this in a foolproof way? (regex_reject_mail_from)
I know this question halfway belongs on serverfault, but basically it's a Perl regex question so I think it fits stackoverflow more.
EDIT:
This should match so I can reject it:
"Someone" <someone#somedomain.com>
This should not match:
"Me" <me#mydomain.com>
This shouldn't match also:
you#mydomain.com
-
I'd suggest the following:
\b[A-Z0-9._%+-]+#(?!mydomain\.com)[A-Z0-9.-]+\.[A-Z]{2,6}\b
Use the /i option to make it case-insensitive.
This will match most valid (and some invalid) e-mail addresses that don't have mydomain.com after the #. Keep in mind that e-mail validation is hard with regexes.
If your regex is going to be applied to the MAIL FROM line in the MTA communication then you do not need to concern yourself with the full 'email address' specification. MAIL FROM lines are just the email address enclosed in '<>', so any regex that tests for #mydomain.com> should work.
\b(?:"[ a-zA-Z]+")?\s*<?[a-zA-Z0-9_.]#(?!mydomain\.com)\w+(?:\.\w{2,})+>?\b
UPDATE: Note that this regex is fa{9,}r from being perfect. Check the official regex for email addresses for more info (Scroll down to the <p/> titled RFC 2822).
Related
I have the follow line of regex (javascript)
/^[a-z0-9_.\-]+#(yahoo|gmail|excite})\.com$/
However, I am unsure of how to make this include subdomains (IF one is present).
So this expression should match uk.yahoo.com and yahoo.com email address as well... How can this be done?
Well, if you want just the subdomain uk.yahoo.com:
/^[a-z0-9_.\-]+#((?:uk\.)?yahoo|gmail|excite)\.com$/
The addition of (?:uk\.)? specifies a optional noncapturing group that matches either 0 or 1 occurrence of the pattern "uk.".
However, using regexes to validate email addresses is an awful idea. RFC2822 is a very complex standard. It's much better to blindly send an email to whatever minimally-validated address the user enters, fail early, and give the user a chance to correct the mistake.
after countless hours googling and trying to contact my webhost (with no positive results) I wanted to jsut 'throw my question out there' and get better expertise with my issue. I really do believe, that this will be helpful to a lot of people as well, stuck asking the same question!
Just to keep things short, we have hosted our email solution with a webhost using cPanel and I have a big requirement. Basically, I need an account level filter to block certain mail addresses from sending out to other mail servers. For example;
lets say we use example.com
user1#example.com can send mail to anyone, anywhere
user2.int#example.com is only allowed to send mail to example.com address but not to any other address, for example gmail.com, yahoo.com, etc.
Out of the options given to me at account level filtering, I thought the best to use is regex.
I'm suspecting that EXIM (default mta for cpanel) uses PCRE like regex expressions, please correct if im wrong.
The syntax i wrote and need help with is the following:
^(?!.+\#example\.com$).*$
With this, all example.com addresses should not match and all other addresses should.
The testing tools I used is https://www.debuggex.com/
Guys, please help and let me know what I am doing wrong. cPanel is letting mail go through and is not blocking it.
The regex:
^(?![^#]*?#example\.com)
should do the trick
How it works
^: Find the beginning of the string/line
(?!...) Assert that it is impossible to find the following regex:
[^#]*? Match all the characters that are not an at symbol (#)
#example\.com Match the exact string '#example\.com'
For a more in-depth explanation see this
I am a email spam quarantine administrator and I can write regular expression rules to block email messages. There is a common classification of email spam hitting our domain such that the username of any of our email addresses is spoofed in front of some other domain.
For example, suppose my email address is jwclark#domain.com. In that case, spammers are writing to me from all kinds of other domains that start with my username such as:
jwclark1234#whatever.com
jwclark#wrongdomain.com
jwclark#a.domain.com
How can I write a regular expression rule to match everything including jwclark and any wildcards, but not match the original jwclark#domain.com? I would like a regex that matches everything above except for my actual example email address jwclark#domain.com.
I've made this regexp here
^jwclark.*[#](?!domain\.com).*$
it's in javascript format, but it should be easy to adapt to php or something else.
Given the nature of your problem, you might be better off making a regex builder function that makes the proper regexp for you, given the parameters.
Or, actually use a different approach. I recently found out how to parse ranges of floating point numbers with regexp, but that doesn't make it the proper solution to finding numbers within ranges. :P
edit - fixed silly redundancy thanks to zx81
edit - change to comply with strange limitations:
^jwclark.{0,25}[#][^d][^o][^m][^a][^i][^n].{0,25}\.com.{0,25}$
demo for the strange one
I'm trying to extract websites without matching email addresses.
In other words if my contact section has
email: a#gmail.com ---- website: www.company.com
I want the www.company.com without matching gmail.com.
So far I have tried everything that I can think of, the best I have so far is
\b(?:.(?<!#))+\.\S+\b
but that will still match gmail.com in a#gmail.com.
I'll admit that my Regex skills are not the strongest, I've done my research regarding negative lookaheads/behinds etc but I still don't know how to do this.
This is an expression made by JGSoft for domain names:
\b(?<!#)((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b
It is internationalized and strict.
I added (?<!#) to stop it from matching domain names after email names.
See a demo here
Some HTML5 input elements accept the pattern attribute, which is a regex for form validation. Some other HTML5 input elements, such as, input type=email does the validation automatically.
Now it seems that the way validation is handled is different accross browsers. Given a specific browser, say Chrome, is it possible to programmatically extract the regex used for validation? Or maybe there is documentation out there?
The HTML5 spec currently lists a valid email address as one matching the ABNF:
1*( atext / "." ) "#" ldh-str *( "." ldh-str )
which is elucidated in this question. #SLaks answer provides a regex equivalent.
That said, with a little digging through the source, shows that WebKit implemented email address validation using basically the same regex as SLaks answer, i.e.,
[a-z0-9!#$%&'*+/=?^_`{|}~.-]+#[a-z0-9-]+(\.[a-z0-9-]+)*
However, there is no requirement that email addresses be validated by a regex. For example, Mozilla (Gecko) implemented email validation using a pretty basic finite state machine. Hence, there needn't be a regex involved in email validation.
The HTML5 spec now gives a (non-normative) regex which is supposed to exactly match all email addresses that it specifies as valid. There's a copy of it on my blog here:
http://blog.gerv.net/2011/05/html5_email_address_regexp/
and in the spec itself:
https://html.spec.whatwg.org/#e-mail-state-(type=email))
The version above is incorrect only in that it does not limit domain components to max 255 characters and does not prevent them beginning or ending with a "-".
Gerv
this works for me:
pattern="[^#]+#[^#]+.[a-zA-Z]{2,6}"