En PHP, si nous avons besoin de faire correspondre quelque chose comme, ["one","two","three"]
, nous pourrions utiliser l'expression régulière suivante avec preg_match
. En utilisant la parenthèse, nous pouvons également extraire les mots un, deux et trois. Je suis conscient de l'objet Matcher
en Java, mais je suis incapable d'obtenir des fonctionnalités similaires; Je suis seulement capable d'extraire la chaîne entière. Comment pourrais-je imiter le comportement preg_match
en Java.Fonctionnalité `preg_match_all` de PHP en Java
Répondre
Avec un Matcher, pour obtenir les groupes, vous devez utiliser la méthode Matcher.group()
.
Par exemple:
Pattern p = Pattern.compile("\\[\"(\\w+)\",\"(\\w+)\",\"(\\w+)\"\\]");
Matcher m = p.matcher("[\"one\",\"two\",\"three\"]");
boolean b = m.matches();
System.out.println(m.group(1)); //prints one
Rappelez-vous group(0)
est la même toute la séquence correspondante.
Ressources:
Java Pcre est un projet qui fournit une implémentation Java de tous les funcions php PCRE. Vous pouvez obtenir quelques idées à partir de là. Vérifiez le projet https://github.com/raimonbosch/java.pcre
Je sais que cet article est sorti de 2010, parce que je l'ai juste cherché, peut-être que quelqu'un d'autre en aurait encore besoin. Alors voici la fonction que j'ai créée pour mon besoin.
essentiellement, il remplacera tous les mots clés avec la valeur d'un JSON (ou modèle, ou toute autre source de données)
comment utiliser:
JsonObject jsonROw = some_json_object;
String words = "this is an example. please replace these keywords [id], [name], [address] from database";
String newWords = preg_match_all_in_bracket(words, jsonRow);
-je utiliser ce code dans mon adaptateur partagé.
public static String preg_match_all_in_bracket(String logos, JSONObject row) {
String startString="\\[", endString="\\]";
return preg_match_all_in_bracket(logos, row, startString, endString);
}
public static String preg_match_all_in_bracket(String logos, JSONObject row, String startString, String endString) {
String newLogos = logos, withBracket, noBracket, newValue="";
try {
Pattern p = Pattern.compile(startString + "(\\w*)" + endString);
Matcher m = p.matcher(logos);
while(m.find()) {
if(m.groupCount() == 1) {
noBracket = m.group(1);
if(row.has(noBracket)) {
newValue = ifEmptyOrNullDefault(row.getString(noBracket), "");
}
if(isEmptyOrNull(newValue)) {
//no need to replace
} else {
withBracket = startString + noBracket + endString;
newLogos = newLogos.replaceAll(withBracket, newValue);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return newLogos;
}
Je suis aussi nouveau pour Java/Android, s'il vous plaît ne hésitez pas à corriger si vous pensez que cela est une mauvaise mise en œuvre ou quelque chose. tks
- 1. regex preg_match | preg_match_all en php
- 2. Traduire le preg_match_all de PHP en Python
- 3. PHP preg_match_all RegEx conflit
- 4. PHP - preg_match_all - iCalendar - REGEX
- 5. PHP preg_match_all() assortis
- 6. PHP preg_match_all html
- 7. PHP/Regex/preg_match_all
- 8. Erreur Fastcgi 500 sur preg_match_all en PHP
- 9. PHP preg_match_all ne retourne rien
- 10. PHP: problème avec preg_match_all() retour
- 11. URL correspondant à l'aide de preg_match_all en PHP et regex
- 12. preg_match_all
- 13. caractères UTF-8 dans preg_match_all (PHP)
- 14. PHP preg_match_all échoue sur les longues chaînes
- 15. PHP: preg_match_all - Impossible d'écrire un travail RegEx
- 16. preg_match_all question
- 17. preg_match_all JS équivalent?
- 18. preg_match_all Problèmes
- 19. preg_match_all expression
- 20. preg_match_all [200932]
- 21. preg_match dans preg_match_all
- 22. PHP: Comment obtenir les index de chaîne d'un preg_match_all?
- 23. php preg_split fonctionnalité
- 24. Fonctionnalité de pool de threads Java
- 25. Quel est l'équivalent C# de preg_match_all?
- 26. Implémentation de la fonctionnalité friend (disponible en C++) dans Java
- 27. Comment répliquer la fonctionnalité de l'instruction C using en Java?
- 28. java Api catalogue? komodoedit fonctionnalité complète auto java
- 29. Fonctionnalité Flex/Bison dans PHP
- 30. Phpwhois de PHP en Java?