Regex: possessive quantifier for the star repetition operator, i.e. \d**

别说谁变了你拦得住时间么 提交于 2019-12-11 11:53:32

问题


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)

回答1:


You can add + to anything to make a possessive quantifier (it's not "doubling the quantifier"). So

System.out.println(in.matches("\\d*+"));


来源:https://stackoverflow.com/questions/16192672/regex-possessive-quantifier-for-the-star-repetition-operator-i-e-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!