java java正则表达式中“\\d+”和“\\d++”的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16183467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:08:38  来源:igfitidea点击:

Difference between "\\d+" and "\\d++" in java regex

javaregex

提问by user2313900

In java, what's the difference between "\\d+"and "\\d++"? I know ++ is a possessive quantifier, but what's the difference in matching the numeric string? What string can match "\\d+"but can't with "\\d++"? Possessive quantifier seems to be significant with quantifier ".*"only. Is it true?

在java中,什么之间的区别"\\d+""\\d++"?我知道 ++ 是一个占有量词,但是匹配数字字符串有什么区别?什么字符串可以匹配"\\d+"但不能匹配"\\d++"?所有格量词似乎".*"仅对量词有意义。这是真的吗?

回答by erickson

Possessive quantifiers will not back off, even if some backing off is required for the overall match.

占有量词不会退让,即使整体匹配需要一些退让。

So, for example, the regex \d++0can never match any input, because \d++will match all digits, including the 0needed to match the last symbol of the regex.

因此,例如,正则表达式\d++0永远无法匹配任何输入,因为\d++它将匹配所有数字,包括0匹配正则表达式最后一个符号所需的数字。

回答by Sumit Singh

\d+ Means:
\dmeans a digit (Character in the range 0-9), and +means 1 or more times. So, \d+is 1 or more digits.

\d+
\d表示表示一个数字(0-9范围内的字符),+表示1次或多次。所以,\d+是 1 位或更多位数字。

\d++ Meansfrom Quantifiers

\d++来自量词的均值

This is called the possessive quantifiers and they always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.

这称为所有格量词,它们总是吃掉整个输入字符串,尝试一次(且仅一次)匹配。与贪婪量词不同,所有格量词永远不会退缩,即使这样做会使整体匹配成功。