最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java正则匹配中文的方法实例分析
时间:2022-06-29 01:34:18 编辑:袖梨 来源:一聚教程网
1、匹配双引号间内容:
代码如下 | 复制代码 |
publicvoidtest1() { // 匹配双引号间内容 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、中文内容也匹配:
代码如下 | 复制代码 |
publicvoidtest2() { // 中文内容也匹配 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、标点也匹配:
代码如下 | 复制代码 |
publicvoidtest3() { // 标点也匹配 Pattern p = Pattern.compile(""([^"|[u4e00-u9fa5ufe30-uffa0]]+)""); Matcher m = p.matcher(""goodjob还有标点!""); System.out.println(m.find() ? m.group(1) :"nothing"); } |
上面三个程序的输出如下:
goodjob
nothing
goodjob里面有中文呢
nothing
goodjob还有标点!
相关文章
- 地下城堡4白茗草怎么获得 白茗草采集位置 09-18
- 燕云十六声狮子坟漠漠喵在哪 狮子坟漠漠喵位置一览 09-18
- 三国谋定天下曹丕阵容怎么配 曹丕阵容搭配推荐 09-18
- 初音未来缤纷舞台兑换码在哪里兑换 兑换码入口介绍 09-18
- 阴阳师雪御前如何配队-雪御前阵容搭配推荐 09-18
- 对决剑之川诸葛玲珑技能及装备如何搭配 09-18