正则如何匹配汉字?

NewHandRookie 2009-08-31 03:02:51
import java.io.*;
import java.util.regex.*;
public class test
{
public static void main(String[] args)
{
String str = "龙";
String regex="\\w";
Matcher m=Pattern.compile(regex).matcher(str);
while(m.find()){
System.out.println(m.group());

}
}
}
import java.io.*;
import java.util.regex.*;
public class test
{
public static void main(String[] args)
{
String str = "龙";
String regex="\\w\\w";
Matcher m=Pattern.compile(regex).matcher(str);
while(m.find()){
System.out.println(m.group());

}
}
}都没有打印结果,不是说\w以匹配任意汉字么
...全文
64 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihao1129 2009-08-31
  • 打赏
  • 举报
回复
用ACSII码试试
angel6709 2009-08-31
  • 打赏
  • 举报
回复
.
loveofmylife 2009-08-31
  • 打赏
  • 举报
回复
package day01;

import java.io.*;
import java.util.regex.*;
public class Test1
{
public static void main(String[] args)
{
String str = "龙的传人";
String regex="[\u4E00-\u9FA5]*";
Matcher m=Pattern.compile(regex).matcher(str);
while(m.find()){
System.out.println(1);
System.out.println(m.group());

}
}
}

结果
龙的传人
yanliang_xt 2009-08-31
  • 打赏
  • 举报
回复

import java.io.*;
import java.util.regex.*;
public class RRR1
{
public static void main(String[] args)
{
String str = "3龙3";
String regex="\\w";
Matcher m=Pattern.compile(regex).matcher(str);
while(m.find()){
System.out.println(m.group());

}
}
}


输出
3
3

分别截取了汉字两边的数。

NewHandRookie 2009-08-31
  • 打赏
  • 举报
回复
匹配不了,你试试,好像说JAVA没提供这个功能我是用命令符运行程序
yanliang_xt 2009-08-31
  • 打赏
  • 举报
回复
\\w
就可以匹配一个汉字

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧