I have a java regex (to validate an email address although this is not what my question is about) which works in an standard java app however when I put that same code into a restful web service it no longer works. The code in the app looks as follows:
public static void main(String[] args) {
List <String> emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
//Invalid emails
emails.add(".username@yahoo.com");
emails.add("username@yahoo.com.");
String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
Pattern pattern = Pattern.compile(regex);
for(String email : emails){
Matcher matcher = pattern.matcher(email);
System.out.println(email +" : "+ matcher.matches());
}
}
This example has been shamelessly copied from here
This actually works fine for my purpose. However when I try to include that same pattern and matcher into a web service I always get a false match. My attempt looks as follows:
@GET
@Path("/{i}")
@Produces(MediaType.APPLICATION_JSON)
public Response getData( @PathParam("i") String eMailAddress_ ) {
String returnStr;
String regex = "^[\\\\w!#$%&'*+/=?`{|}~^-]+(?:\\\\.[\\\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\\\.)+[a-zA-Z]{2,6}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(eMailAddress_);
if (matcher.matches()) {
returnStr = eMailAddress_ + " is VALID";
}
else {
returnStr = eMailAddress_ + " is INVALID";
}
return Response.ok("{\"Result\":\"" + returnStr + "\"}", MediaType.APPLICATION_JSON).build();
}
Now if I send user@domain.com on the url the response I get in return from the matcher is FALSE.
I have even tried to hard code eMailAddress_ to be user@domain.com in the webservice so that no matter what parameter I pass the webservice applies the matcher to that hard coded value which I know validates to TRUE in the first app however I still get the matcher returning FALSE.
I am using netbeans 8.2 on java 1.8.
Aucun commentaire:
Enregistrer un commentaire