Tag: regex

正则表达式validation只包含字符(非特殊字符),空格和数字的字符串

我正在使用Ruby on Rails 3.0.9,我想validation一个只能包含字符(不是特殊字符 – 不区分大小写),空格和数字的字符串。 在我的validation码中,我有: validates :name, :presence => true, :format => { :with => regex } # Here I should set the ‘regex’ 我该如何陈述正则表达式?

如何在ruby中使用正则表达式的utf8

在RoR中,如何使用utf8代码validation中文或日语单词的发布表单。 在GBK代码中,它使用[\ u4e00- \ u9fa5] +来validation中文单词。 在Php中,它使用/ ^ [\ x {4e00} – \ x {9}} + $ / u来表示utf-8页面。

在Ruby的情况下使用带有正则表达式匹配的命名Captures ……什么时候?

我想使用命名捕获来解析用户输入以提高可读性。 当他们键入命令时,我想捕获一些参数并传递它们。 我在case语句中使用RegExps,因此我无法分配/pattern/.named_captures的返回/pattern/.named_captures 。 这是我希望能够做到的(例如): while command != “quit” print “Command: ” command = gets.chomp case command when /load (?\w+)/ load(filename) end end

Ruby正则表达式 – gsub存储匹配的内容吗?

如果我使用 .gsub(/matchthisregex/,”replace_with_this”) gsub存储它与正则表达式匹配的地方吗? 我想在替换字符串中使用匹配的内容。 比如像 “replace_with_” + matchedregexstring + “this” 在上面的例子中,matchedregexstring将是来自gsub的存储匹配? 对不起,如果那令人困惑,我不知道怎么说。

Ruby正则表达式在文本文件中添加一行

我需要匹配输入的文本文件字符串中的一行,并将该捕获的行包裹在一个字符中。 例如,想象一个文本文件: test foo test bar 我想用gsub输出: XtestX XfooX XtestX XbarX 我在匹配线路时遇到了麻烦。 我尝试使用正则表达式从^开始并以$结尾,但它似乎不起作用。 有任何想法吗? 我有一个文本文件,其中包含以下内容: test foo test bag 正在将文本文件作为命令行参数读入。 所以我得到了 string = IO.read(ARGV[0]) string = string.gsub(/^(test)$/,’X\1X’) puts string 它输出与文本文件完全相同的内容。

如何更改此正则表达式以从未指定v参数的Youtube URL获取YoutubevideoID?

目前我正在使用此正则表达式从Youtubeurl中提取videoID: url.match(/v=([^&]*)/)[1] 如何更改此设置,以便它还可以从此Youtubeurl获取videoID,该url没有v参数: http://www.youtube.com/user/SHAYTARDS#p/u/9/Xc81AajGUMU 谢谢阅读。 编辑:我正在使用ruby 1.8.7

基于正则表达式的Rails自定义validation?

我有以下正则表达式,我在我的routes.rb中使用/ type-in​​-something-here # AZ, az, 0-9, _ in the middle but never starting or ending in a _ # At least 5, no more than 500 characters 在路线中,这适用于: match ‘:uuid’ => ‘room#show’, :constraints => { :uuid => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ } 我想将此作为validation,因此不会创建无效记录。 所以我在room.rb中添加了以下内容: validates_format_of :uuid, :with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i, :message => “Invalid! Alphanumerics only.” 但是这个validates_format_of不起作用,而不是添加错误,它允许记录保存。 有什么想法有什么不对吗? 谢谢

Anchor \ z和\ G如何在Ruby中运行?

我使用的是Ruby1.9.3。 我是这个平台的新手。 从文档中我刚刚得到了两个anchor ,它们是\z和\G 现在我和\z一起玩它看看它是如何工作的,因为定义( 字符串的 结束或结束 )让我感到困惑,我无法理解它的含义 – 由End 。 所以我尝试了下面的小片段。 但仍然无法抓住。 码 irb(main):011:0> str = “Hit him on the head me 2\n” + “Hit him on the head wit> => “Hit him on the head me 2\nHit him on the head with a 24\n” irb(main):012:0> str =~ /\d\z/ => nil irb(main):013:0> str = […]

使用\ d扫描字符串中的Unicode数字

根据Oniguruma文档 , \d字符类型匹配: 十进制数字char Unicode:General_Category – Decimal_Number 但是,在包含所有Decimal_Number字符的字符串中扫描\d导致只匹配拉丁0-9位数: #encoding: utf-8 require ‘open-uri’ html = open(“http://www.fileformat.info/info/unicode/category/Nd/list.htm”).read digits = html.scan(/U\+([\da-f]{4})/i).flatten.map{ |s| s.to_i(16) }.pack(‘U*’) puts digits.encoding, digits #=> UTF-8 #=> 0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨… p RUBY_DESCRIPTION, digits.scan(/\d/) #=> “ruby 1.9.2p180 (2011-02-18) [i386-mingw32]” #=> [“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”] 我误读了文档吗? 为什么不匹配其他Unicode数字,和/或有没有办法让它这样做?

Nokogiri:使用XPath搜索

我使用Nokogiri (Rubygem)css搜索在我的html中查找某些 。 看起来Nokogiri的css搜索不喜欢正则表达式。 我想切换到Nokogiri的xpath搜索,因为它似乎支持搜索字符串中的正则表达式。 如何在xpath搜索中实现下面提到的(伪)css搜索? require ‘rubygems’ require ‘nokogiri’ value = Nokogiri::HTML.parse(<<-HTML_END) " A B Bla C D 3 ” HTML_END # my_block is given my_bl = “1” # my_eq corresponds to this regex my_eq = “\/[0-9]+\/” # FIXME The following line should be changed to an xpath search. if my_div = value.css(“div#eq-#{my_eq}_bl-#{my_bl}.foo”).first # […]