jjzjj

pattern-matching

全部标签

ruby - Ruby 中的通配符字符串匹配

我想编写一个实用函数/模块,为字符串提供简单的通配符/全局匹配。我不使用正则表达式的原因是用户最终会使用某种配置文件提供匹配模式。我找不到任何这样稳定的gem-试过joker,但设置有问题。我正在寻找的功能很简单。例如,给定以下模式,这里是匹配项:pattern|test-string|match========|=====================|====================*hn|john,johnny,hanna|true,false,false#wildcard,similarto/hn$/i*hn*|john,johnny,hanna|true,true,

ruby - 模式匹配时 =~ 和 match() 有什么区别?

我正在使用Ruby1.9.3。我在玩一些模式,发现了一些有趣的东西:示例1:irb(main):001:0>/hay/=~'haystack'=>0irb(main):003:0>/st/=~'haystack'=>3示例2:irb(main):002:0>/hay/.match('haystack')=>#irb(main):004:0>/st/.match('haystack')=>#=~返回其第一个匹配项的第一个位置,而match返回模式。除此之外,=~和match()还有什么区别吗?执行时间差(根据@Casper)irb(main):005:0>quickbm(10000000

ruby - 如何获取字符串中所有出现的模式的索引

string="JackandJillwentupthehilltofetchapailofwater.Jackfelldownandbrokehiscrown.AndJillcametumblingafter."d=string.match(/(jack|jill)/i)#->MatchData"Jill"1:"Jill"d.size#->1这只匹配它看起来第一次出现的地方。string.scan完成了部分工作,但它没有说明任何有关匹配模式索引的信息。如何获取模式的所有匹配实例及其索引(位置)的列表? 最佳答案 可以使用.scan

Ruby String#scan 相当于返回 MatchData

正如问题标题中所述,Ruby字符串上是否有等效于String#Scan的方法?但不是只返回每个匹配项的列表,而是返回一个MatchData数组?例如:#Matchesasetofcharactersbetweenunderscorepairs"foo_bar__baz_hashbang".some_method(/_[^_]+_/)#=>[#<MatchData"_bar_"&rt,<MatchData"_baz_"&rt]或者任何我能得到相同或相似结果的方法都是好的。我想这样做是为了找到Ruby字符串中“字符串”的位置和范围,例如"goodbyeand"world"insid

ruby-on-rails - 在 Rails 3 中使用 current_page 时为 "No routes matches"

有没有人遇到过使用current_page时路由神秘地变得无法检测到?在Rails3中?即使使用包含路由、View和Controller的完全生成的脚手架,我也会收到“无路由匹配”错误。代码如下:ifcurrent_page?(:controller=>'users',:action=>"show")如果我向routes.rb添加一个“匹配”命令,它工作正常,但如果资源已经创建,为什么我需要这样做呢?我错过了什么? 最佳答案 如果你只是想测试当前的Controller,你可以这样做:ifparams[:controller]=='u

ruby - 为什么 =~ 运算符只是有时有副作用?

我注意到Ruby/Oniguruma中的副作用仅出现在4个看似等效的语句中的1个中。为什么变量是day在009中定义,但不在003中,005或007?irb(main):001:0>r=/(?\d\d):(?\d\d)/=>/(?\d\d):(?\d\d)/irb(main):002:0>r=~"24:12"=>0irb(main):003:0>dayNameError:undefinedlocalvariableormethod`day'irb(main):004:0>"24:12"=~r=>0irb(main):005:0>dayNameError:undefinedlocalva

ruby-on-rails - 验证失败 : Upload file has an extension that does not match its contents

我正在使用回形针gem上传文件。我的回形针gem版本是paperclip-4.1.1。上传文件时抛出Validationfailed:Uploadfilehasanextensionthatdoesnotmatchitscontents.我正在尝试上传xlsx文件。而且我已经在模型content_type中提到了这一点。validates_attachment_content_type:upload_file,:content_type=>%w(application/mswordapplication/vnd.ms-officeapplication/vnd.ms-excelappl

ruby-on-rails - 错误 "' Validate_default_type !': An option' s default must match its type (ArgumentError)"when running Ruby on Rails generate on Windows

我正在关注thistutorial并且刚刚开始。我已经使用geminstallrails安装了RubyonRails,并使用railsnewblog创建了一个博客。教程现在说我需要运行railsgeneratecontrollerWelcomeindex,但是当我这样做时,我得到了这个错误:C:/Ruby22/lib/ruby/gems/2.2.0/gems/thor-0.19.2/lib/thor/parser/option.rb:130:in`validate_default_type!':Anoption'sdefaultmustmatchitstype.(ArgumentErr

ruby - Regexp.last_match 线程安全吗?

这是我正在查看的代码:defmethod_missing(id,*args)returnself.find(Regexp.last_match(1),args[0])ifid.id2name=~/find_by_(.+)/raiseNoMethodErrorend如果我有多个线程调用Regexp.last_match会怎样?如果我有多个线程使用method_missing方法调用对象会怎样? 最佳答案 Ruby1.9.2平台文档声明调用Regexp.last_match等同于读取特殊的$~全局变量。摘自“TheRubyProgram

ruby-on-rails - Rails - 如何将变量插入正则表达式 (Regex),例如 assert_match

我想做类似的事情assert_match/blahblahblah#{@user}/,@some_text但我运气不好。我在这里做错了什么? 最佳答案 这是将变量插入正则表达式的正确方法:irb(main):001:0>a='Hi'=>"Hi"irb(main):002:0>b=/Not#{a}/=>/NotHi/所以您的问题很可能是断言由于匹配不当而失败。检查@user和@some_text的值并尝试http://rubular.com想出一个匹配的正则表达式 关于ruby-on-ra