【问题标题】:Ruby Match Result Not Working on HerokuRuby 匹配结果在 Heroku 上不起作用
【发布时间】:2012-06-25 03:52:46
【问题描述】:

使用在 Heroku 上运行的 Rails,我遇到了这样一种情况,即在字符串中找到了正则表达式匹配,但没有为括号中的组设置 $1 变量。这只发生在我在 Heroku 上的生产设置中,而不是在我的本地系统上(带有 Ruby ruby​​-1.9.2-p0 的 Rails 3.0.7)。代码是

puts "**** gateway_reply=#{gateway_reply}"
match = gateway_reply =~ /ID: (\w+)/
msg_id = $1
puts "**** match=#{match}, msg_id ($1) = #{$1}"

在我的本地系统上,输出是

**** gateway_reply=ID: da2x7s5tjumtxtnk1krl8wps4wpasiee
**** match=0, msg_id ($1) = da2x7s5tjumtxtnk1krl8wps4wpasiee

在生产系统上,$1 没有设置:

**** gateway_reply=ID: 93e4ca3590207761af6f3b5ba3545b36
**** match=0, msg_id ($1) = 

关于这里发生了什么的任何答案?

【问题讨论】:

  • 你在 heroku 上使用什么堆栈?您确定它与您的开发环境匹配吗?
  • 您可能希望使用\s+ 代替正则表达式中的空格。一个空间可以根据环境有许多不同的值
  • 这是个好主意,Michael,尽管由于表达式匹配,它仍应返回 $1 中的值,不是吗? Aubrey,栈是 aspen-mri-1.8.6,*bake-mri-1.9.2,bamboo-ree-1.8.7,cedar。某些版本的 Ruby 是否对匹配的返回值有不同的行为?

标签: ruby-on-rails ruby regex heroku pattern-matching


【解决方案1】:

为我工作:

> gateway_reply='ID: da2x7s5tjumtxtnk1krl8wps4wpasiee'
> gateway_reply.match(/ID: (\w+)/)
> $1
=> "da2x7s5tjumtxtnk1krl8wps4wpasiee"

> gateway_reply =~ /ID: (\w+)/
=> 0
> $1
=> "da2x7s5tjumtxtnk1krl8wps4wpasiee"

请注意匹配运算符~的结果是匹配的位置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2014-02-08
    • 2015-08-16
    • 2013-11-23
    相关资源
    最近更新 更多