天天看點

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

}