Java教程中有正则表达式量词的示例和描述。
贪婪 - 吃完完整的字符串,然后退后一个字符,然后再试一次
Regex: .*foo // greedy String to search: xfooxxxxxxfoo Found "xfooxxxxxxfoo"
不情愿 - 从头开始,然后一次吃一个角色
Regex: .*?foo // reluctant quantifier String to search: xfooxxxxxxfoo Found "xfoo", "xxxxxxfoo"
占有 - 吃掉整个字符串,尝试一次匹配
Regex: .*+foo // possessive quantifier String to search: xfooxxxxxxfoo No match found
他们没问题,我理解他们,但是有人可以向我解释当正则表达式更改为字符类时会发生什么?还有其他规则吗?
Regex: [fx]* String to search: xfooxxxxxxfoo Found "xf","","","xxxxxxf","","","","" Regex: [fx]*? String to search: xfooxxxxxxfoo Found 15 zero-length matches Regex: [fx]*+ String to search: xfooxxxxxxfoo Found "xf","","","xxxxxxf","","","",""