0
点赞
收藏
分享

微信扫一扫

利用正则表达式提取括号内内容

M4Y 2022-03-11 阅读 55
java
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestZz {
    public static void main(String[] args) {
        String str = "中华人民共和国,简称(中国)。";
        Matcher mat = Pattern.compile("(?<=\\()(\\S+)(?=\\))").matcher(str);//此处是中文输入的()
        String str1 = null;
        String str2 = null;
        while (mat.find()) {
            str2 = mat.group();
            System.out.println(mat.group());
        }
        System.out.println(str);
        str1 = str.replace(str2, "");
        System.out.println("one:"+str1+"two:"+str2);

        str2 = "("+str2+")";
        str1 = str.replace(str2, "");
        System.out.println("one:"+str1+"two:"+str2);

    }
}

https://www.cnblogs.com/manliu/p/4004696.html

举报

相关推荐

0 条评论