2

I was experimenting to use this regex pattern checking method as described in

How to check if a given Regex is valid?

import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

public class RegexTester {
    public static void main(String[] arguments) {
        String userInputPattern = arguments[0];
        try {
            Pattern.compile(userInputPattern);
       } catch (PatternSyntaxException exception) {
            System.err.println(exception.getDescription());
            System.exit(1);
        }
       System.out.println("Syntax is ok.");
    }
}

(using Google Web ToolKit) but I keep getting the "failed" message":

Plugin failed to connect to Development Mode server at 127.0.0.1:9997

whenever I use the Pattern.compile(String) method.

Does anyone know what I'm doing wrong?

Community
  • 1
  • 1
footprint.
  • 21
  • 3

2 Answers2

2

You should use 'com.google.gwt.regexp.shared.RegExp', instead of 'java.util.regex.*'.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

Some characters have special meaning they are known as Metacharacters. so providing a sample code below.

I will suggest you to visit metacharacters and java.util.regex.Pattern

String s = "+1234567890PrasadBelhe1234567890";
System.out.println("args[0]:"+args[0]);
if(args[0].startsWith("+")||args[0].startsWith("?")||args[0].startsWith("*")){
    System.out.println("Invalid regex :"+args[0]);//added basic
    System.exit(1);
}
else{
    if(args[0].equals(".classpath")) args[0]=".";//java default
    //Pattern p = Pattern.compile(Pattern.quote(args[0]));
    Pattern p = Pattern.compile(args[0]);
    Matcher mymacher = p.matcher(s);
    System.out.println("Finding results...");
    while(mymacher.find()) System.out.println(s.substring( mymacher.start(), mymacher.end()));
    System.out.println("Syntax is ok:"+args[0]);
}
Belhe001
  • 89
  • 9