Java Regex debugging help.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Java Regex debugging help.

Zachary1234-2
-My String is correct, but I can't say the same for my regex code.
How can I update my expression to return true and match the supplied String?


package Test;

import java.util.regex.*;
import static java.lang.System.*;

public class Test {

     private static final Pattern AGGREGATE_MULTIPLES =
                          Pattern.compile("^((-d)\\s\\d\\s)+(-n)\\s\\d$");
     
     public static void main(String ... args)
    {
      String data = new String("-d 2 -d 10 -n 2"); //This needs to match.
     
      out.println(AGGREGATE_MULTIPLES.matcher(data).matches());
     
     
    }
   
}




Reply | Threaded
Open this post in threaded view
|

Re: Java Regex debugging help.

Eduard Karel de Jong
It seems that the \\d in the pattern is lacking a quantifier (+, *), and
so it doesn't match the 10 after the second -d in the string.

--
Eduard

Zachary1234 wrote:

> -My String is correct, but I can't say the same for my regex code.
> How can I update my expression to return true and match the supplied String?
>
>
> package Test;
>
> import java.util.regex.*;
> import static java.lang.System.*;
>
> public class Test {
>
>       private static final Pattern AGGREGATE_MULTIPLES =
>                            Pattern.compile("^((-d)\\s\\d\\s)+(-n)\\s\\d$");
>
>       public static void main(String ... args)
>      {
>        String data = new String("-d 2 -d 10 -n 2"); //This needs to match.
>
>        out.println(AGGREGATE_MULTIPLES.matcher(data).matches());
>
>
>      }
>
> }
>
>
>
>
>