Closed AshwathSatyanarayan closed 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.
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 :)
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 (?
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.
I found the issue, I was using an older maven version. It is working now. Thanks for the help! Much appreciated.
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.