Regex: possessive quantifier for the star repetition operator, i.e. \d**
From the GLib Reference Manual, section "Regular expression syntax", subsection "Atomic grouping and possessive quantifiers":
Consider the pattern d+foo
when applied to the string 123456bar
: after matching all 6 digits and then failing to match "foo", the normal action of the matcher is to try again with only 5 digits matching the d+ item, and then with 4, and so on, before ultimately failing.
If we use (?>d+)foo
(called atomic grouping) for the previous example, the matcher give up immediately on failing to match "foo" the first time.
When the subpattern for an atomic group is just a single repeated item, as in the example above, a simpler notation, called a "possessive quantifier" can be used: d++foo
My question is: is there any reason why there is no equivalent for the star ( *
) repetition operator?
Example in Java:
final String in = "123456";
// "plus" (+)
System.out.println(in.matches("d+")); // true
System.out.println(in.matches("(?>d+)")); // true
System.out.println(in.matches("d++")); // true
// "star" (*)
System.out.println(in.matches("d*")); // true
System.out.println(in.matches("(?>d*)")); // true
System.out.println(in.matches("d**")); // exception
The exception stack trace is:
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)
You can add +
to anything to make a possessive quantifier (it's not "doubling the quantifier"). So
System.out.println(in.matches("d*+"));
链接地址: http://www.djcxy.com/p/76978.html