email validation in a tmap components with perl regular expression

One Star

email validation in a tmap components with perl regular expression

Hi,
Using TOS to pull leads information from a database I created a routine with perl regular expression to validate email adress.
For this I used the following code :
------------------
use Exporter;
use vars qw(@EXPORT @ISA);
@ISA = qw(Exporter);
@EXPORT = qw(
isemail
);
sub isemail {
if (@_ =~ m/^+@+$/) {
return 1;
}else{
return 0;
}
}
1;
----------------
Once done it is easy to call this routine from your tMap component.
Of course you can improve the perl regular expression Smiley Wink
Regards
Cyril
Tags (1)
Employee

Re: email validation in a tmap components with perl regular expression

Of course you can improve the perl regular expression Smiley Wink

That's my job :-)
sub isEmail {
use Regexp::Common qw;

if ($_ =~ m/$RE{Email}{Address}/) {
return 1;
}

return 0;
}

You have to install Regexp::Common::Email::Address
One Star

Re: email validation in a tmap components with perl regular expression

Great it's working !
Where can we find what exactly this regexp check ?
You have to install Regexp::Common::Email::Address

ppm is my friend Smiley Wink
Employee

Re: email validation in a tmap components with perl regular expression

Where can we find what exactly this regexp check ?

print $RE{Email}{Address}, "\n";

(?:(?-xism:(?:(?-xism:(?-xism:(?-xism:(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*:;@\,."\s]+(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|\.|\s*"(?-xism:(?-xism:)|(?-xism:\\(?-xism:)))+"\s*)+)|(?:(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*:;@\,."\s]+(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*"(?-xism:(?-xism:)|(?-xism:\\(?-xism:)))+"(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*))+))?(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*<(?-xism:(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*(?-xism::;@\,."\s]+(?:\.:;@\,."\s]+)*)(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*"(?-xism:(?-xism:)|(?-xism:\\(?-xism:)))+"(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*))\@(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*(?-xism::;@\,."\s]+(?:\.:;@\,."\s]+)*)(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*\\\])|(?-xism:\\(?-xism:))))*\s*\](?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)))>(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*))|(?-xism:(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*(?-xism::;@\,."\s]+(?:\.:;@\,."\s]+)*)(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*"(?-xism:(?-xism:)|(?-xism:\\(?-xism:)))+"(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*))\@(?-xism:(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*(?-xism::;@\,."\s]+(?:\.:;@\,."\s]+)*)(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*)|(?-xism:(?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*\\\])|(?-xism:\\(?-xism:))))*\s*\](?-xism:(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)|\s+)*))))(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|(?-xism:\s*\((?:\s*(?-xism:(?-xism:(?>+))|(?-xism:\\(?-xism:))|))*\s*\)\s*)))*\s*\)\s*)*))
One Star

Re: email validation in a tmap components with perl regular expression

Hum,
I am migrating my jobs to Java. How can i code the same email validation routine in Java ?
Any idea ?
Cyril
One Star

Re: email validation in a tmap components with perl regular expression

This could match :
//template routine Java
package routines;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public class Validation {

public static boolean isValideEmail(String email) {
Perl5Matcher matcher = new Perl5Matcher();
Perl5Compiler compiler = new Perl5Compiler();
Pattern pattern;

try {
pattern = compiler.compile("^+@+\\.+$");
if (!matcher.matches(email, pattern)) {
return false;
}
} catch (MalformedPatternException e) {
throw new RuntimeException(e);
}
return true;
}
}

But you need to add a lib ("jakarta-oro-2.0.8.jar") with a right clic on your routine (Edit routine librairies)
Cyril
Employee

Re: email validation in a tmap components with perl regular expression

Hello,
Your routine is working well, but for such a regexp, you can also simply use the java.util.regex.Pattern which is directly available in Standard Java.
Regards,
Four Stars

Re: email validation in a tmap components with perl regular expression

Hi,
here is my java code for the same utility : It works great with common error cases, except with an input string as "test@test.c", it returns true !
I'm not fluent with Regexp but this pattern seems to be a standard for email checking. It works properly on the RegExp editor in Talend, but not anymore once compiled in a job.

import java.util.regex.Pattern;
public class EmailChecking {
/**
* {talendTypes} String
*
* {Category} EmailChecking
*
* {param} string("destinataire@company.com") input: The email adress to be checked
*
* {example} isEmailValid("bidule@ machin.com ") # false || isEmailValid("truc@machin.com ") # true
*/
public static boolean isEmailValid(String email) {

boolean b = Pattern.matches("\b+@+\.{2,4}\b", email);

return b;
}
}

If someone has an idea ..