I am using Ruby 1.9.3
. I was playing with some patterns and found something interesting:
我使用的是Ruby 1.9.3。我正在玩一些模式并发现一些有趣的东西:
Example 1:
例1:
irb(main):001:0> /hay/ =~ 'haystack'
=> 0
irb(main):003:0> /st/ =~ 'haystack'
=> 3
Example 2:
例2:
irb(main):002:0> /hay/.match('haystack')
=> #<MatchData "hay">
irb(main):004:0> /st/.match('haystack')
=> #<MatchData "st">
=~
returns the first location of its first match, whereas match
returns the pattern. Other than that, is there any difference between =~
and match()
?
=〜返回其第一个匹配的第一个位置,而匹配返回该模式。除此之外,=〜和match()之间有什么区别吗?
Execution time difference (As per @Casper)
执行时间差(根据@Casper)
irb(main):005:0> quickbm(10000000) { "foobar" =~ /foo/ }
Rehearsal ------------------------------------
8.530000 0.000000 8.530000 ( 8.528367)
--------------------------- total: 8.530000sec
user system total real
8.450000 0.000000 8.450000 ( 8.451939)
=> nil
irb(main):006:0> quickbm(10000000) { "foobar".match(/foo/) }
Rehearsal ------------------------------------
15.360000 0.000000 15.360000 ( 15.363360)
-------------------------- total: 15.360000sec
user system total real
15.240000 0.010000 15.250000 ( 15.250471)
=> nil
2 个解决方案
#1
7
First make sure you're using the correct operator: =~
is correct, ~=
is not.
首先确保你使用正确的运算符:=〜是正确的,〜=不是。
The operator =~
returns the index of the first match (nil
if no match) and stores the MatchData
in the global variable $~
. Named capture groups are assigned to a hash on $~
, and, when the RegExp
is a literal on the left side of the operator, are also assigned to local variables with those names.
operator =〜返回第一个匹配的索引(如果没有匹配则为nil)并将MatchData存储在全局变量$〜中。命名捕获组被分配给$〜的散列,并且当RegExp是运算符左侧的文字时,也会将其分配给具有这些名称的局部变量。
>> str = "Here is a string"
>> re = /(?<vowel>[aeiou])/ # Contains capture group named "vowel"
>> str =~ re
=> 1
>> $~
=> #<MatchData "e" vowel:"e">
>> $~[:vowel] # Accessible using symbol...
=> "e"
>> $~["vowel"] # ...or string
=> "e"
>> /(?<s_word>\ss\w*)/ =~ str
=> 9
>> s_word # This was assigned to a local variable
=> " string"
The method match
returns the MatchData
itself (again, nil
if no match). Named capture groups in this case, on either side of the method call, are assigned to a hash on the returned MatchData
.
方法匹配返回MatchData本身(如果不匹配则再次返回nil)。在这种情况下,在方法调用的任一侧,命名捕获组被分配给返回的MatchData上的哈希。
>> m = str.match re
=> #<MatchData "e" vowel:"e">
>> m[:vowel]
=> "e"
See http://www.ruby-doc.org/core-1.9.3/Regexp.html (as well as the sections on MatchData
and String
) for more details.
有关更多详细信息,请参阅http://www.ruby-doc.org/core-1.9.3/Regexp.html(以及MatchData和String部分)。
#2
3
When you have a method that does not modify state, all that matters is the return value. So what's the difference between red and blue, besides color? My point is that this is kind of a strange question, one which you seem to already know the answer to.
(@sawa set me straight here)
当你有一个不修改状态的方法时,重要的是返回值。那么除了颜色之外,红色和蓝色之间的区别是什么?我的观点是,这是一个奇怪的问题,你似乎已经知道了答案。 (@sawa把我直接放在这里)
But that said, both methods return nil
(a falsy value) when the regex does not match. And, both methods return a truthy value when it does match. =~
returns an integer that represents the first character of the match, and even if that is 0
, because 0
is truthy in Ruby. match
returns an object with very detailed match data, which is handy when you want lots of info about the match.
但是,当正则表达式不匹配时,两种方法都返回nil(一个假值)。并且,两种方法在匹配时都会返回真值。 =〜返回一个表示匹配的第一个字符的整数,即使它是0,因为0在Ruby中是真的。 match返回一个具有非常详细的匹配数据的对象,当您需要大量有关匹配的信息时,这很方便。
=~
is typically used in conditionals, when you only care if something matches:
=〜通常用于条件,当你只关心某些东西匹配时:
do_stuff if "foobar" =~ /foo/
do_stuff if "foobar".match(/foo/) # same effect, but probably slower and harder to read
match
is typically used when you want details about what was matched:
当您需要有关匹配内容的详细信息时,通常会使用匹配:
name = "name:bob".match(/^name:(\w+)$/)[1]
puts name #=> 'bob'
#1
7
First make sure you're using the correct operator: =~
is correct, ~=
is not.
首先确保你使用正确的运算符:=〜是正确的,〜=不是。
The operator =~
returns the index of the first match (nil
if no match) and stores the MatchData
in the global variable $~
. Named capture groups are assigned to a hash on $~
, and, when the RegExp
is a literal on the left side of the operator, are also assigned to local variables with those names.
operator =〜返回第一个匹配的索引(如果没有匹配则为nil)并将MatchData存储在全局变量$〜中。命名捕获组被分配给$〜的散列,并且当RegExp是运算符左侧的文字时,也会将其分配给具有这些名称的局部变量。
>> str = "Here is a string"
>> re = /(?<vowel>[aeiou])/ # Contains capture group named "vowel"
>> str =~ re
=> 1
>> $~
=> #<MatchData "e" vowel:"e">
>> $~[:vowel] # Accessible using symbol...
=> "e"
>> $~["vowel"] # ...or string
=> "e"
>> /(?<s_word>\ss\w*)/ =~ str
=> 9
>> s_word # This was assigned to a local variable
=> " string"
The method match
returns the MatchData
itself (again, nil
if no match). Named capture groups in this case, on either side of the method call, are assigned to a hash on the returned MatchData
.
方法匹配返回MatchData本身(如果不匹配则再次返回nil)。在这种情况下,在方法调用的任一侧,命名捕获组被分配给返回的MatchData上的哈希。
>> m = str.match re
=> #<MatchData "e" vowel:"e">
>> m[:vowel]
=> "e"
See http://www.ruby-doc.org/core-1.9.3/Regexp.html (as well as the sections on MatchData
and String
) for more details.
有关更多详细信息,请参阅http://www.ruby-doc.org/core-1.9.3/Regexp.html(以及MatchData和String部分)。
#2
3
When you have a method that does not modify state, all that matters is the return value. So what's the difference between red and blue, besides color? My point is that this is kind of a strange question, one which you seem to already know the answer to.
(@sawa set me straight here)
当你有一个不修改状态的方法时,重要的是返回值。那么除了颜色之外,红色和蓝色之间的区别是什么?我的观点是,这是一个奇怪的问题,你似乎已经知道了答案。 (@sawa把我直接放在这里)
But that said, both methods return nil
(a falsy value) when the regex does not match. And, both methods return a truthy value when it does match. =~
returns an integer that represents the first character of the match, and even if that is 0
, because 0
is truthy in Ruby. match
returns an object with very detailed match data, which is handy when you want lots of info about the match.
但是,当正则表达式不匹配时,两种方法都返回nil(一个假值)。并且,两种方法在匹配时都会返回真值。 =〜返回一个表示匹配的第一个字符的整数,即使它是0,因为0在Ruby中是真的。 match返回一个具有非常详细的匹配数据的对象,当您需要大量有关匹配的信息时,这很方便。
=~
is typically used in conditionals, when you only care if something matches:
=〜通常用于条件,当你只关心某些东西匹配时:
do_stuff if "foobar" =~ /foo/
do_stuff if "foobar".match(/foo/) # same effect, but probably slower and harder to read
match
is typically used when you want details about what was matched:
当您需要有关匹配内容的详细信息时,通常会使用匹配:
name = "name:bob".match(/^name:(\w+)$/)[1]
puts name #=> 'bob'