In the GLib Reference Guide, section "Regular Expression Syntax" , subsection "Atomic Grouping and Possessive Quantifiers":
Consider the pattern \d+foowhen applied to a string 123456bar: after matching all 6 digits and then matching with "foo", the normal action of the match is to retry using just 5 digits corresponding to \ d + and then 4, etc. before eventually failing.
If we use (?>\d+)foo(called an atomic grouping) for the previous example, the match is immediately discarded if the first time does not match "foo".
When the subpattern for an atomic group is just one repeated element, as in the example above, you can use a simpler notation called the "possessive quantifier": \d++foo
My question is: is there a reason why there is no equivalent for the star repetition operator ( *)?
An example in Java:
final String in = "123456";
System.out.println(in.matches("\\d+"));
System.out.println(in.matches("(?>\\d+)"));
System.out.println(in.matches("\\d++"));
System.out.println(in.matches("\\d*"));
System.out.println(in.matches("(?>\\d*)"));
System.out.println(in.matches("\\d**"));
Exception stack trace:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 3
\d**
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.sequence(Pattern.java:1878)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.util.regex.Pattern.matches(Pattern.java:928)
at java.lang.String.matches(String.java:2090)
sp00m source
share