天天看点

JAVA正则中文匹配

 1、匹配双引号间内容:

Java代码

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

}

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、中文内容也匹配:

Java代码

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

}

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、标点也匹配:

Java代码

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

}