Home » Php » PHP's `preg_match_all` functionality in Java

PHP's `preg_match_all` functionality in Java

Posted by: admin July 12, 2020 Leave a comment

Questions:

In PHP if we need to match a something like, ["one","two","three"], we could use the following regular expression with preg_match.

$pattern = "/\[\"(\w+)\",\"(\w+)\",\"(\w+)\"\]/"

By using the parenthesis we are also able to extract the words one, two and three. I am aware of the Matcher object in Java, but am unable to get similar functionality; I am only able to extract the entire string. How would I go about mimicking the preg_match behaviour in Java.

How to&Answers:

With a Matcher, to get the groups you have to use the Matcher.group() method.

For example :

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

Remember group(0) is the same whole matching sequence.

Example on ideone


Resources :

Answer:

Java Pcre is a project that provides a Java implementation of all the php pcre funcions. You can get some ideas from there. Check the project https://github.com/raimonbosch/java.pcre

Answer:

I know this post came from 2010, buat the fact that I just searched for it, may be someone else would still need it too. So here is the function I created for my need.

basically, it will replace all keywords with the value from a json (or model, or any data source)

how to use:

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);

I use this codes in my shared adapter.

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;
}

I am also new to Java/Android, please feel free to correct if you think this is a bad implementation or something. tks