tags:

views:

145

answers:

4

Hi folks,

I have a little program allowing users to type-in some regular expressions. afterwards i like to check if this input is a valid regex or not.

I'm wondering if there is a build-in method in Java, but could not find such jet.

Can you give me some advice?

Best regards Phil

A: 

You Can User regexbuddy software form this.....

RegexBuddy is your perfect companion for working with regular expressions. Easily create regular expressions that match exactly what you want. Clearly understand complex regexes written by others. Quickly test any regex on sample strings and files, preventing mistakes on actual data. Debug without guesswork by stepping through the actual matching process. Use the regex with source code snippets automatically adjusted to the particulars of your programming language. Collect and document libraries of regular expressions for future reuse. GREP (search-and-replace) through files and folders. Integrate RegexBuddy with your favorite searching and editing tools for instant access.

You can download it Here

Gurpreet Singh
thank you for the hint. this looks like a real powerful tool to create and explore regex'. but i think this is not what i'm searching for.first, it is shareware (EUR 30)second, it seems NOT to have a java API. I'm not looking for a tool to create and check my own regexes, but a java lib which can be used at runtime of my program, when a user inserts a new regex.
Philipp Andre
Hmmm ... can I smell tinned meat product??
Stephen C
RegexBuddy is something Philipp's customers might use to help them with creating regular expressions for use with Philipp's application. RegexBuddy can emulate the Java flavor. But it doesn't solve Phulipp's problem. Catching the exception thrown by `Pattern.compile` is the way to go.
Jan Goyvaerts
@Stephen: I am the only person who derives income from RegexBuddy who is active on stackoverflow.com and I always post under my own name. I don't know who Gurpreet is. I do have some actual tinned meat in the kitchen, but you can't have it! :-)
Jan Goyvaerts
+3  A: 

You can just Pattern.compile the regex string and see if it throws PatternSyntaxException.

    String regex = "***";
    PatternSyntaxException exc = null;
    try {
        Pattern.compile(regex);
    } catch (PatternSyntaxException e) {
        exc = e;
    }
    if (exc != null) {
        exc.printStackTrace();
    } else {
        System.out.println("Regex ok!");
    }

This one in particular produces the following output:

java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
***
^

Regarding lookbehinds

Here's a quote from the old trusty regular-expressions.info:

Important Notes About Lookbehind

Java takes things a step further by allowing finite repetition. You still cannot use the star or plus, but you can use the question mark and the curly braces with the max parameter specified. Java recognizes the fact that finite repetition can be rewritten as an alternation of strings with different, but fixed lengths.

I think the phrase contains a typo, and should probably say "different, but finite lengths". In any case, Java does seem to allow alternation of different lengths in lookbehind.

    System.out.println(
        java.util.Arrays.toString(
            "abracadabra".split("(?<=a|ab)")
        )
    ); // prints "[a, b, ra, ca, da, b, ra]"

There's also a bug in which you can actually have an infinite length lookbehind and have it work, but I wouldn't rely on such behaviors.

    System.out.println(
        "1234".replaceAll(".(?<=(^.*))", "$1!")
    ); // prints "1!12!123!1234!"
polygenelubricants
Jap exactly what i'm looking for. Thank you!I'm surprised, my example with differing lookup length passes this test. is the java regex engine now able to handle that?!
Philipp Andre
@Philipp: added things about lookbehinds. Check out the second example =)
polygenelubricants
I was looking for that on regular-expressions.info too. Good find!
laz
No, that's not a typo. He's saying *each* (theoretical) alternative has a fixed length. If they weren't fixed, you wouldn't be able to determine which one was longest.
Alan Moore
jap, i was wrong with my example in the comment somewhere at the top of this discussion. polygenelubricants definitely got the point! java allows differing length, but not star or plus notation in lookforward/lookbehind. his code example works for me as well!
Philipp Andre
+4  A: 

Here is an example.

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.");
    }
}

java RegexTester (capture outputs "Unclosed group" for example.

laz
thanks! see my comment on polygenelubricants post.
Philipp Andre
+1  A: 

Most obvious thing to do would be using compile method in java.util.regex.Pattern and catch PatternSyntaxException

String myRegEx;
...
...
Pattern p = Pattern.compile(myRegEx);

This will throw a PatternSyntaxException if myRegEx is invalid.

ring bearer