vendredi 19 octobre 2018

Regex Expression worked on a online tool but not in java [duplicate]

This question already has an answer here:

I am trying to match a emoji code in json from a string so i can replace it with html code. This is how the string looks like "\uD83D\uDE42".

This is my regex expression that i wrote "\u[0-9]0-9(\u([a-z]|[A-Z])([0-9]|[a-z])([a-z]|[A-Z]|[0-9]))?"

I tested this expression in an online tool on multiple patterns it worked but in android code it does not detect the pattern.

Moreover the testing string could be like this \uD83D\uDE42" or \ud83d\ude42" or "\u270a" or "\ud83d\udcaa" or even this "\ud83d\udc4a". On an online tool (https://regexr.com/).

This is the code i wrote

 String stringToSearch = "Four score and seven years ago our  \uD83D\uDE42 fathers ...";

Pattern p = Pattern.compile("\\[u][A-Z][0-9][0-9][A-Z]\\[u][A-Z][A-Z][0-9][0-9]");   // the pattern to search for
Matcher m = p.matcher(stringToSearch);

// now try to find at least one match
if (m.find())
  System.out.println(stringToSearch+"\nFound a match");
else
  System.out.println(stringToSearch+"\nDid not find a match");

Aucun commentaire:

Enregistrer un commentaire