Tag: 正则表达式

如何在Ruby Regex中使用AND

我遇到Ruby正则表达式的问题。 你如何在ruby中做和(和)正则表达式? 例如: cat and dog cat dog I just want to match “cat and dog”

RSpec使用正则表达式启动匹配变量

是否存在RSpec start_with匹配器的变体,在检查字符串数组时将使用正则表达式匹配而不是相等? (我不介意自己编写;但我不想重新发明轮子。) 具体来说,我希望有一个看起来像这样的规范: it ‘begins with the standard header’ do output = method_under_test # output is an array of Strings expect(output).to start_with([/foo/, /bar/]) end 如果output[0]匹配/foo/并且output[1]匹配/bar/则此规范应该通过。 假设我确实需要编写自己的匹配器,有没有办法“重载” start_with ,还是需要选择其他名称?

Ruby Regex用于字符串中的重复数字

如果我有一个像”123123123″这样的字符串 – 这里123重复3次。 那么我怎样才能在ruby中只获得”123″ ? 2.所以如果字符串是”12312312″ – 这里123重复2次然后只有12 ,所以这里仍然需要得到”123″ 。 3.即使字符串是99123123123 ,我仍然需要获得123 。 这可能在Ruby Regex中有用吗? 编辑:我希望这能解决Project Euler Problem 26 。 所以这里123可以是任何东西。 我想要的是提取1个至少2个重复数字。

如何添加用户模型validation以阻止某些电子邮件域

我希望有一个经过validation的电子邮件域列表,以防止在我的应用程序上注册。 10minutemail.com,yopmail.com,mail.com,mail.ru等… 我的用户模型中有一个域列表,如下所示: BAD_DOMAINS = [/10minutemail.com/, /yopmail.com/, /mail/ 我想在电子邮件字段中添加用户validation,以便在用户注册其中一个域时添加错误。 BAD_DOMAINS.each { |rule| return true if !domain.match(rule).nil? } 我有正则表达式工作,但如何将其作为validation添加? 我试过这个: validates :email, :format => { : exclusion => BAD_DOMAINS, :message => “%{value} no good.” } 谢谢

路由约束不会正确地为Rails参数赋值; 使用’+’分隔值

我希望能够在我的应用程序路由中支持国家/地区代码和区域代码。 例如: /实体/美 /实体/我们+ CA /实体/ US /分钟 /实体/ US / MN +无线+ IA /实体/我们+ CA / BC + WA 我目前的路线: get “/entities/:country_code/(:region_code)” => “entities#index”, :constraints => {:country_code=>/[a-zA-Z]{2}[\+\,]?/, :region_code=>/[a-zA-Z]{2}[\+\,]?/} resources :entities 尝试/entities/us+ca导致此exception: # Use callbacks to share common setup or constraints between actions. def set_entity @entity = Entity.find(params[:id]) end Application Trace | Framework Trace | […]

Rails:为什么“格式化”(正则表达式)validation失败?

我有以下产品价格validation: class Product true, :format => PRICE_REGEX … end 它应该允许价格从0到999999.99 。 但是,如果我输入hello ,则validation通过,并将0.00保存在数据库中。 但是:presencevalidation工作正常。 我在这里想念的是什么?

正则表达式 – 在推文中查找所有链接

我的正则表现很差,让我失望所以这里的一些帮助会很棒。 我想做的就是返回推文中出现的所有链接(只是一个字符串) – 一些例子是: “Great summary http://mytest.com/blog/post.html (#test) “http://mytest.com/blog/post.html (#test) “post: http://mytest.com/blog/post.html” 它还应该支持多个链接,例如: “read http://mytest.com/blog/post.html and http://mytest.com/blog/post_two.html” 任何帮助都会很棒! 谢谢 本

Rails 3validationIPv4和IPv6格式

我知道IPv4和IPv6的validation格式。 但不确定如何将它们组合在一起,因此至少有一种格式应该是真的。 这是我的validation validates :src_ip_addr, :presence => true, :uniqueness => true, :format => { :with => Resolv::IPv4::Regex, :message => “Not an valid IPv4 format”} validates :src_ip_addr, :presence => true, :uniqueness => true, :format => { :with => Resolv::IPv6::Regex, :message => “Not an valid IPv6 format”} 如何将它们组合起来如果一种格式正确则validation应该有效。 仅当ipv4和ipv6格式不正确时才会失败。 谢谢。

使用ruby解析字符串路径名

我有一个类似于的路径字符串: /some/long/path/filename.extension 我需要在ruby中解析“filename”部分

计算railsvalidation中的单词

我正在使用以下validation来计算rails中的单词(我从Rails文档中得到了示例),但它并不真实: validates :body, :length => { :minimum => 50, :maximum => 300, :tokenizer => lambda { |str| str.scan(/\w+/) }, :too_short => “must have at least %{count} words”, :too_long => “must have at most %{count} words” } 一个用户试图发布一个291个单词的东西(这是Word给出的计数)并且它被拒绝的时间太长了。 我不确切地知道正在使用的表达式有什么问题,或者可能是一个很好的表达式来确保准确的字数。