PHP's `preg_match_all` functionality in Java

扶醉桌前 提交于 2019-12-04 01:19:04

问题


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.


回答1:


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 :

  • Javadoc - Matcher.group()



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3842112/phps-preg-match-all-functionality-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!