Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Close on Regular Expression !match

I am having a problem with the following bit of code. I am trying to match a string. When I have a match everything works perfectly. When it does not find a match it throws an exception and causes a force close.


Code Snippet

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: main
02-17 14:12:11.322: ERROR/AndroidRuntime(3854): java.lang.IllegalStateException: No successful match so far
02-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$2.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
like image 540
Bill Mote Avatar asked Feb 17 '11 14:02

Bill Mote


People also ask

What is ?! In regex?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.

How do you stop greedy in regex?

You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" .

How do you match parentheses in regex?

The way we solve this problem—i.e., the way we match a literal open parenthesis '(' or close parenthesis ')' using a regular expression—is to put backslash-open parenthesis '\(' or backslash-close parenthesis '\)' in the RE. This is another example of an escape sequence.


2 Answers

Try checking for a match instead:

if (m.matches()){

instead of:

if (m.group(0) != ""){
like image 126
qbert220 Avatar answered Sep 24 '22 00:09

qbert220


http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html#find() the find method returns true if and only if a match is found. So before you call you have to ensure that matches are found.

if (m.find()) {
    // do other stuff
}
like image 33
Mojo Risin Avatar answered Sep 25 '22 00:09

Mojo Risin