工作生活

andorid 正則匹配大括号{}

2019-07-02  本文已影响0人  Pino
// 匹配包括大括号在内的字符串
    public static SpannableString getStr1(String content) throws Exception {
        SpannableString spanText = new SpannableString(content);
        Pattern pattern = Pattern.compile("\\{\\[[^\\}]*\\]\\}");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String target = matcher.group();
            Log.e("test",target);
        }
        return spanText;
    }
getStr1("sdd啊实打实大{[@opo,123]}对方{哈哈,444}水电费{[@ppp,9898]}是")
// 打印结果{[@opo,123]} {[@ppp,9898]}
// 下面是匹配括号里面的字符串
 public static SpannableString getStr2(String content) throws Exception {
        SpannableString spanText = new SpannableString(content);
        Pattern pattern = Pattern.compile("(?<=\\{\\[)(.*?)(?=\\]\\})");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String target = matcher.group();
            Log.e("test",target);
        }
        return spanText;
    }

getStr2("sdd啊实打实大{[@opo,123]}对方{哈哈,444}水电费{[@ppp,9898]}是")
// 打印结果@opo,123 @ppp,9898
上一篇下一篇

猜你喜欢

热点阅读