简体   繁体   English

这种正则表达式的简化是否正确?

[英]Is this regular expression simplification correct?

I was reading the "How Browsers Work" article here: How Browsers Work (It's a great read.) 我正在阅读这里的“浏览器如何工作”一文: 浏览器如何工作 (这是一个很好的阅读。)

But at one point, they mention this as a rule of their grammar defining an integer: 但有一次,他们提到这是他们定义整数的语法规则:

INTEGER :0|[1-9][0-9]*

Is it, or is it not, exactly the same (and simpler) to write: 它是,或者不是,完全相同(和更简单)写:

INTEGER :[1-9]*[0-9]

I couldn't think of a case that did not satisfy both rules, nor a reason why the first rule would be preferred. 我想不出一个不满足这两个规则的案例,也不是第一条规则首选的原因。

Is there a reason for including a simple base case (such as 0 ), or is it just pedantics? 是否有理由包含一个简单的基本案例(如0 ),还是只是迂腐?

These two expressions are different: the first one will take 101 , but the second one wouldn't. 这两个表达式是不同的:第一个表达式需要101 ,而第二个表达式不会。 The expression from the book is pretty good at matching integers while disallowing leading zeros. 本书中的表达式非常适合匹配整数而不允许前导零。

They are not the same, as my simplification can only include one zero, and only if it is a trailing zero. 它们不一样,因为我的简化只能包含一个零,并且只有它是一个尾随零。 Eg. 例如。 this case does not match for the simplified rule: 这种情况与简化规则不匹配:

101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM