mjuricek1 Absent Member.
Absent Member.
2569 views

validate email addresses and exclude some by regex


Hello,
I need to validate email address in the update user form but I am facing
with one issue. I need to allow email addresses only but not emails from
specific domains. e.g. acme.com and acme.eu
If I have check for email addresses only,everything is working but if I
add a check for "acme.com" and "acme.eu" (see second regex below), SSPR
says that email address is not valid. But interesting is that the
expression looks good. I tested it on https://regex101.com/

this check the emails and this works but I need to exclude some
domains:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

and this does not work in SSPR/PWM:
(?=^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)(?!^[a-zA-Z0-9._%+-]+@acme\.com)(?!^[a-zA-Z0-9._%+-]+@acme\.eu)

How can I write an expression which will validate email addresses and
will filter some specific emails (something like exclude list of email
addresses)?

Regards,
Milan


--
mjuricek
------------------------------------------------------------------------
mjuricek's Profile: https://forums.netiq.com/member.php?userid=1616
View this thread: https://forums.netiq.com/showthread.php?t=56344

0 Likes
4 Replies
Knowledge Partner
Knowledge Partner

Re: validate email addresses and exclude some by regex

mjuricek wrote:

> If I have check for email addresses only,everything is working but if I
> add a check for "acme.com" and "acme.eu" (see second regex below), SSPR
> says that email address is not valid. But interesting is that the
> expression looks good. I tested it on https://regex101.com/


regex101.com tests php and javascript regex, SSPR is written mostly in
jsp/java, which seems to make a difference in this case. There are a lot of
regex flavors out there, with lots of subtle differences. For use with IDM I
usually use http://www.regexplanet.com/advanced/java/index.html which seems to
be good for SSPR as well.

> this check the emails and this works but I need to exclude some
> domains:
> ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$


This works because it's a simple positive match describing a valid email
address and matching the full input string.

> and this does not work in SSPR/PWM:
> (?=^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)(?!^[a-zA-Z0-9._%+-]+@acme
> \.com)(?!^[a-zA-Z0-9._%+-]+@acme\.eu)


This does not work, because you are using lookaheads, which are assertions and
do not match any part of the input string (just like ^ and $) but rather a
position in a string.

> How can I write an expression which will validate email addresses and
> will filter some specific emails (something like exclude list of email
> addresses)?


Now it gets interesting. Java provides a couple of regex-related functions with
slight differences and SSPR is most likely using String.matches() or
Matcher.matches() here, which only returns true if the whole input string
matches your regex.
Since lookarounds do not match any part of the string you need to

a) add information at which position in the string you want to look ahead plus
b) a pattern that matches the whole string.

Adding a ^ in front (to look at the input string from the beginning) and a .*
at the end (to match the whole string) should already get you what you need.

You could then also remove the three ^ at the beginning of the individual
lookaheads, pull out the first part of the email address, use \w and \d
shorthands instead of the long character classes and default to
case-insensitive matching. Finally pull the exceptions together into a single
negative lookahead:

(?i)^[\w.%+-]+@(?=[a-z\d.-]+\.[a-z]{2,}$)(?!acme\.(com|eu)$).*

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)
0 Likes
Knowledge Partner
Knowledge Partner

Re: validate email addresses and exclude some by regex

Lothar Haeger wrote:

> (?i)^[\w.%+-]+@(?=[a-z\d.-]+\.[a-z]{2,}$)(?!acme\.(com|eu)$).*


I just realize that the ^and the first lookahead are not even necessary:

(?i)[\w.%+-]+@(?!acme\.(com|eu)$)[a-z\d.-]+\.[a-z]{2,}

Regex are endless fun... 😉

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)
0 Likes
mjuricek1 Absent Member.
Absent Member.

Re: validate email addresses and exclude some by regex


works, thanks.
Milan


--
mjuricek
------------------------------------------------------------------------
mjuricek's Profile: https://forums.netiq.com/member.php?userid=1616
View this thread: https://forums.netiq.com/showthread.php?t=56344

0 Likes
Knowledge Partner
Knowledge Partner

Re: validate email addresses and exclude some by regex

lhaeger;2435681 wrote:
Lothar Haeger wrote:

> (?i)^[\w.%+-]+@(?=[a-z\d.-]+\.[a-z]{2,}$)(?!acme\.(com|eu)$).*


I just realize that the ^and the first lookahead are not even necessary:

(?i)[\w.%+-]+@(?!acme\.(com|eu)$)[a-z\d.-]+\.[a-z]{2,}

Regex are endless fun... 😉

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)


Totally agree on the regex fun and thanks for the link.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.