简体   繁体   English

来自变量的Ruby regexp

[英]Ruby regexp from variable

Using Ruby 1.8.7 I want to store some regular expressions in the database and have them be easily reconstituted as a Regexp object when I need to use them for validation. 使用Ruby 1.8.7我想在数据库中存储一些正则表达式,当我需要使用它们进行验证时,可以将它们重新构建为Regexp对象。 I've found that Ruby exhibits some unwanted behavior. 我发现Ruby表现出一些不必要的行为。 For instance: 例如:

r = Regexp.new(/^\w+$/i)        => /^\w+$/i
r_i = r.inspect                 => "/^\\w+$/i"
r_s = r.to_s                    => "(?i-mx:^\\w+$)"
r_from_r_i = Regexp.new(r_i)    => /\/^\w+$\/i/
r_from_r_s = Regexp.new(r_s)    => /(?i-mx:^\w+$)/
r == r_from_r_i                 => false
r == r_from_r_s                 => false

What I'd like is to be able to store the Regexp in the database in the form /pattern/options instead of the (?options:pattern) format, because I'm more familiar with the former. 我想要的是能够以表格/pattern/options而不是(?options:pattern)格式将Regexp存储在数据库中,因为我对前者比较熟悉。 But, when creating a new Regexp from a variable, the pattern gets all wacky. 但是,当从变量创建新的Regexp ,模式变得古怪。 Ruby is escaping the / s - so the pattern is invalid. Ruby正在逃避/ s - 因此模式无效。

Is there any way to make Ruby honor the /pattern/options format when parsing a regular expression? 在解析正则表达式时,有没有办法让Ruby尊重/pattern/options格式?

You can do as below: 你可以这样做:

r = Regexp.new(/^\w+$/i) # => /^\w+$/i
source,option = r.source,r.options # => ["^\\w+$", 1]
regex = Regexp.new(source,option)
regex == r # => true

options

Returns the set of bits corresponding to the options used when creating this Regexp 返回与创建此Regexp时使用的选项对应的位集

source

Returns the original string of the pattern 返回模式的原始字符串

Now, if you want a single variable to hold all your Regex details, then consider as below: 现在,如果您想要一个变量来保存所有正则表达式的详细信息,请考虑如下:

r = Regexp.new(/^\w+$/i) # => /^\w+$/i
source_option = r.source,r.options # => ["^\\w+$", 1]
regex = Regexp.new(*source_option)
regex == r # => true

Why did yours get false with to_s or inspect ? 为什么你的to_sinspect false

Read the doc carefully; 仔细阅读文档; you can see that Regexp.to_s is saying: 你可以看到Regexp.to_s在说:

Returns a string containing the regular expression and its options (using the (?opts:source) notation. This string can be fed back in to Regexp::new to a regular expression with the same semantics as the original. (However, Regexp#== may not return true when comparing the two, as the source of the regular expression itself may differ, as the example shows). Regexp#inspect produces a generally more readable version of rxp. 返回一个包含正则表达式及其选项的字符串(使用(?opts:source)表示法。这个字符串可以反馈到Regexp :: new到具有与原始语义相同语义的正则表达式。(但是, Regexp#==当比较两者时, Regexp#==可能不会返回true ,因为正则表达式本身的来源可能不同,如示例所示。) Regexp#inspect会生成一个通常更易读的rxp版本。

Now, see: 现在,请参阅:

r = Regexp.new(/^\w+$/i) # => /^\w+$/i
r_s = r.to_s
r_from_r_s = Regexp.new(r_s)
r == r_from_r_s # => false
# because the source strings are not same for r and r_from_r_s
r.source # => "^\\w+$"
r_from_r_s.source # => "(?i-mx:^\\w+$)"

The same explanation as above holds for the result of r == r_from_r_i . 与上述相同的解释适用于r == r_from_r_i的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM