最新下载
热门教程
- 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还有标点!
相关文章
- 波场 05-02
- 狗狗币能涨到5万美刀吗 05-02
- 游戏dau下降 05-02
- btc行情 05-02
- 从零开始学虚拟货币交易 05-02
- 比特币实时交易策略 05-02