package com.tool;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTh {
public static void main(String[] args) {
String paramValue="你好啊,1233333,不好啊";
System.out.println(PatternTh.getChinese(paramValue));
}
/*1、至少匹配一个汉字的写法。
2、这两个unicode值正好是Unicode表中的汉字的头和尾。
3、"[]"代表里边的值出现一个就可以,后边的“+”代表至少出现1次,合起来即至少匹配一个汉字。
*/
public static String getChinese(String paramValue) {
String regex = "([\u4e00-\u9fa5]+)";
String str = "";
Matcher matcher = Pattern.compile(regex).matcher(paramValue);
while (matcher.find()) {
str+= matcher.group(0);
}
return str;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTh {
public static void main(String[] args) {
String paramValue="你好啊,1233333,不好啊";
System.out.println(PatternTh.getChinese(paramValue));
}
/*1、至少匹配一个汉字的写法。
2、这两个unicode值正好是Unicode表中的汉字的头和尾。
3、"[]"代表里边的值出现一个就可以,后边的“+”代表至少出现1次,合起来即至少匹配一个汉字。
*/
public static String getChinese(String paramValue) {
String regex = "([\u4e00-\u9fa5]+)";
String str = "";
Matcher matcher = Pattern.compile(regex).matcher(paramValue);
while (matcher.find()) {
str+= matcher.group(0);
}
return str;
}
}
本文介绍了一个简单的Java程序,该程序利用正则表达式从字符串中提取所有的中文字符。通过定义一个特定的正则表达式,程序能够准确地识别并返回输入字符串中的所有汉字。

1638

被折叠的 条评论
为什么被折叠?



