简体   繁体   English

Ruby 1.8.7而不是2.0中的正则表达式错误?

[英]Regex error in Ruby 1.8.7 but not 2.0?

In Ruby 1.8.7 the following regex warning: nested repeat operator + and * was replaced with '*'. 在Ruby 1.8.7中,以下正则表达式警告:嵌套的重复运算符+和*被替换为'*'。

^(\w+\.\w+)\|(\w+\.\w+)\n+*$

It does work in Ruby 2.0 though? 它可以在Ruby 2.0中使用吗?

http://rubular.com/r/nRUSP5LNZA http://rubular.com/r/nRUSP5LNZA

A nested operator works, but is warned because it is useless. 嵌套运算符可以工作,但是会被警告,因为它没有用。 \\n+* means: \\n+*表示:

  • Zero or more repeatition of 零个或多个重复
  • One or more repeatition of 一个或多个重复
  • \\n

which is equivalent to a more simple expression \\n* , which means: 等效于更简单的表达式\\n* ,这意味着:

  • Zero or more repeatition of 零个或多个重复
  • \\n

There is no reason to use \\n+* . 没有理由使用\\n+* Ruby regex engine was replaced in Ruby 1.9 and in Ruby 2.0, and if there are any differences, then it is simply that the newer engine does not check for warnings as the older one did. Ruby regex引擎已在Ruby 1.9和Ruby 2.0中被替换,并且如果存在任何差异,则仅仅是较新的引擎不会像较早的引擎那样检查警告。

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

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