`
kinkding
  • 浏览: 147907 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JAVA正则中文匹配

    博客分类:
  • JAVA
阅读更多

1、匹配双引号间内容:

    public void test1() {
        // 匹配双引号间内容
        String pstr = "\"([^\"]+)\"";
        Pattern p = Pattern.compile(pstr);
        Matcher m = p.matcher("\"goodjob\"");
        System.out.println(m.find() ? m.group(1) : "nothing");

        // 测试中文
        m = p.matcher("\"goodjob里面有中文呢\"");
        System.out.println(m.find() ? m.group(1) : "nothing");
    }

  

 

2、中文内容也匹配:

    public void test2() {
        // 中文内容也匹配
        String pstr = "\"([^\"|[\u4e00-\u9fa5]]+)\"";
        Pattern p = Pattern.compile(pstr);
        Matcher m = p.matcher("\"goodjob里面有中文呢\"");
        System.out.println(m.find() ? m.group(1) : "nothing");

        // 测试标点
        m = p.matcher("\"goodjob还有标点!\"");
        System.out.println(m.find() ? m.group(1) : "nothing");
    }

 

 

3、标点也匹配:

    public void test3() {
        // 标点也匹配
        Pattern p = Pattern.compile("\"([^\"|[\u4e00-\u9fa5\ufe30-\uffa0]]+)\"");
        Matcher m = p.matcher("\"goodjob还有标点!\"");
        System.out.println(m.find() ? m.group(1) : "nothing");
    }

 

 

上面三个程序的输出如下:

goodjob
nothing
goodjob里面有中文呢
nothing
goodjob还有标点!

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics