我的Android进阶之旅------>解决错误: java.util.regex.PatternSyntaxException: Incorrect Unicode property

来源:互联网 发布:汽车数据平台 编辑:程序博客网 时间:2024/05/21 10:21

1、错误描述

今天使用正则表达式验证密码的时候,报了错误

java.util.regex.PatternSyntaxException: Incorrect Unicode property near index 32:

错误具体输出日志如下:

04-28 12:49:46.254 2224-2224/com.xtc.watch E/AndroidRuntime: FATAL EXCEPTION: main                                                             Process: com.xtc.watch, PID: 2224                                                             java.util.regex.PatternSyntaxException: Incorrect Unicode property near index 32:                                                             ^(?![0-9]+$)(?![a-zA-Z]+$)(?!\pP+$)(\S){6,16}$                                                                                             ^                                                                 at java.util.regex.Pattern.compileImpl(Native Method)                                                                 at java.util.regex.Pattern.compile(Pattern.java:411)                                                                 at java.util.regex.Pattern.<init>(Pattern.java:394)                                                                 at java.util.regex.Pattern.matches(Pattern.java:424)                                                                 at java.lang.String.matches(String.java:1334)

该正则表达式的代码如下所示:

 String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\pP+$)(\\S){6,16}$";

2、解决方法

通过百度,查到一篇文章解决了该bug。该文章链接地址是:
http://stackoverflow.com/questions/22665156/incorrect-unicode-property-near-index

尝试把 \pP改成\p{P}

即将上面报错的错误正则表达式

  String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\pP+$)(\\S){6,16}$";

变成正确的正则表达式

String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!\\p{P}+$)(\\S){6,16}$";

这样就解决了该bug。

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng

这里写图片描述

1 0
原创粉丝点击