Ruby:正则表达式中的正则表达式
发布时间:2020-12-17 03:00:03 所属栏目:百科 来源:网络整理
导读:我是 Ruby的新手.我正在试图弄清楚如何编写嵌套的case语句.这是我想要做的一个例子: # cucumber,webrat paths.rb def path_to(page_name) case page_name when /the "?(.*)"? event registration homepage/ then case $1 when '2011 CIO GL Global' then '/
我是
Ruby的新手.我正在试图弄清楚如何编写嵌套的case语句.这是我想要做的一个例子:
# cucumber,webrat paths.rb def path_to(page_name) case page_name when /the "?(.*)"? event registration homepage/ then case $1 when '2011 CIO GL Global' then '/event/index/id/236' when '2011 CIO IS Chicago' then '/event/index/id/275' when '2011 CIO ES Denver' then '/event/index/id/217' when '2011 CIO ES Vancouver,BC' then '/event/index/id/234' else raise "Can't find mapping for "#{$1}" to a path.n" + "Now,go and add a mapping in #{__FILE__}" end when /the contact match verification page/ '/attendee/user-verification' end end 在我的功能文件中,它说: When I go to the "2011 CIO IS Chicago" event registration homepage 它在这一步失败了,因为它引发了上面提到的异常,即使我在上面的case语句中定义了它.我究竟做错了什么? 解决方法
是的,第二个问号是混淆正则表达式.
表达方式: .*b? 可以用两种方式匹配字符串“axb”,或者.*匹配整个事物,或者匹配’ax’和’b?’.匹配’b’.正则表达式算法是“贪婪的” – 它尽可能早地匹配尽可能多的匹配. 我将正则表达式重写为: when /the "?([^"]*)"? event registration homepage/ then 确保$1不会以任何双引号结束…… (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |