android – 强制关闭正则表达式!匹配

android – 强制关闭正则表达式!匹配,第1张

概述我遇到以下代码问题.我想匹配一个字符串.当我有一场比赛时,一切都很完美.如果找不到匹配项,则会抛出异常并导致强制关闭. 代码片段 private void validatePhoneNumberFormat(String t){ Pattern p = Pattern.compile("^Match this exactly!$"); Matcher m = p.matcher(t 我遇到以下代码问题.我想匹配一个字符串.当我有一场比赛时,一切都很完美.如果找不到匹配项,则会抛出异常并导致强制关闭.

代码片段

private voID valIDatePhoneNumberFormat(String t){    Pattern p = Pattern.compile("^Match this exactly!$");    Matcher m = p.matcher(t);        m.find();    if (m.group(0) != ""){        this.myString = m.group(0);        this.setIsValID(true);        this.setStatus(0);    } else {        this.myString = "InvalID input String";        this.setIsValID(false);        this.setStatus(99); //  String parsing error    }}

logcat的

02-17 14:12:10.562: WARN/dalvikvm(3854): threadID=1: thread exiting with uncaught exception (group=0x40015560)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854): FATAL EXCEPTION: main02-17 14:12:11.322: ERROR/AndroIDRuntime(3854): java.lang.IllegalStateException: No successful match so far02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at java.util.regex.Matcher.ensureMatch(Matcher.java:607)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at java.util.regex.Matcher.group(Matcher.java:358)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.example.RegExTest.MyList.valIDateMyStringFormat(MyList.java:47)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.example.RegExTest.MyList.<init>(MyList.java:15)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.example.RegExTest.DataManagerActivity.readinputfile(DataManagerActivity.java:128)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.example.RegExTest.DataManagerActivity.onClick(DataManagerActivity.java:77)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.vIEw.VIEw.performClick(VIEw.java:2485)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.vIEw.VIEw$PerformClick.run(VIEw.java:9080)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.os.Handler.handleCallback(Handler.java:587)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.os.Handler.dispatchMessage(Handler.java:92)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.os.Looper.loop(Looper.java:123)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at androID.app.ActivityThread.main(ActivityThread.java:3647)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at java.lang.reflect.Method.invokeNative(Native Method)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at java.lang.reflect.Method.invoke(Method.java:507)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:597)02-17 14:12:11.322: ERROR/AndroIDRuntime(3854):     at dalvik.system.NativeStart.main(Native Method)02-17 14:12:11.402: WARN/ActivityManager(62):   Force finishing activity com.androID.example.RegExTest/.DataManagerActivity
解决方法 请尝试检查匹配:
if (m.matches()){

代替:

if (m.group(0) != ""){
总结

以上是内存溢出为你收集整理的android – 强制关闭正则表达式!匹配全部内容,希望文章能够帮你解决android – 强制关闭正则表达式!匹配所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存