florianingerl / com.florianingerl.util.regex

Java regex library
MIT License
45 stars 5 forks source link

Recursive Regex #5

Closed AshwathSatyanarayan closed 5 years ago

AshwathSatyanarayan commented 5 years ago

String regex = "(?(DEFINE)(?(?group2)!)(?([\d\w\s]*)letters))(?group1)";

This will match 'asdasdletters' i.e. that ends with letters Group 1 uses group 2, but group 2 group definition comes afterwards. This leads to an exception.

Exception in thread "main" com.florianingerl.util.regex.PatternSyntaxException: Unknown inline modifier near index 22

(?(DEFINE)(?(?group2)!)(?([\d\w\s]*)letters))(?group1) at com.florianingerl.util.regex.Pattern.error(Pattern.java:2560) at com.florianingerl.util.regex.Pattern.group0(Pattern.java:3583) at com.florianingerl.util.regex.Pattern.sequence(Pattern.java:2666) at com.florianingerl.util.regex.Pattern.expr(Pattern.java:2600) at com.florianingerl.util.regex.Pattern.group0(Pattern.java:3495) at com.florianingerl.util.regex.Pattern.sequence(Pattern.java:2666) at com.florianingerl.util.regex.Pattern.expr(Pattern.java:2600) at com.florianingerl.util.regex.Pattern.group0(Pattern.java:3536) at com.florianingerl.util.regex.Pattern.sequence(Pattern.java:2666) at com.florianingerl.util.regex.Pattern.expr(Pattern.java:2600) at com.florianingerl.util.regex.Pattern.compile(Pattern.java:2302) at com.florianingerl.util.regex.Pattern.(Pattern.java:1959) at com.florianingerl.util.regex.Pattern.compile(Pattern.java:1595)

Please help me out here.

florianingerl commented 5 years ago

Sorry, I am afraid that I don't understand your problem. Do you want to match strings like "letters" or "asdletters" or "asdasdletters" ? Then you can do that with java.util.regex and use String regex = "(asd)*letters"

Please explain a bit more of what you want to do if you still need help.

AshwathSatyanarayan commented 5 years ago

Yes sure. I am trying to validate JSON using a regex I found online.

$pcre_regex = ' / (?(DEFINE) (?<number> -? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)? ) (?<boolean> true | false | null ) (?<string> " ([^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " ) (?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \] ) (?<pair> \s* (?&string) \s* : (?&json) ) (?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \} ) (?<json> \s* (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) \s* ) ) \A (?&json) \Z /six ';

This is the regex I found. As you can see 'array' uses 'json' group whose definition comes later on. When I try to use this regex, I get an exception. Similarly I tried the below regex where group1 uses group2 whose definition comes later on. String regex = "(?(DEFINE)(?<group1>(?group2)!)(?<group2>([\d\w\s]*)letters))(?group1)";

Does your library support this kind of regex? Thanks in advance :)

florianingerl commented 5 years ago

The following program runs on my computer

            String regex= "(?(DEFINE)(?<group1>(?'group2'))(?<group2>(asd)*letters))(?'group1')";

    Pattern p = Pattern.compile(regex);

    if( p.matcher("letters").matches() )
        System.out.println("Worked like a charm!");
    if( p.matcher("asdletters").matches() )
        System.out.println("Worked like a charm!");
    if( p.matcher("asdasdletters").matches() )
        System.out.println("Worked like a charm!");

and prints three times "Worked like a charm!" to the console.

To define a new group with name group you use the syntax (?stuff-that-this-group-should-match ). To recurse to this group, you use the syntax (?'groupname'). Be aware that the syntax for regex is different in each programming language and library. The syntax of this library is explained here https://docs.oracle.com/javase/9/docs/api/java/util/regex/Pattern.html and in this github article.

To match you Json-stuff, you would need to write (?'json') instead of (?&json) for example. But you would need to change a lot of other stuff too. That's simply necessary because the pcre-regex-syntax is different from the syntax of java.util.regex or of com.florianingerl.util.regex.

AshwathSatyanarayan commented 5 years ago

I found the issue, I was using an older maven version. It is working now. Thanks for the help! Much appreciated.