简体   繁体   English

在Ruby中,=〜和/ \是什么意思?

[英]What do =~ and /\ mean in Ruby?

I've been teaching myself Ruby and for a certain problem I'm trying to solve I notice a lot of people are using =~ and /\\ in their code. 我一直在教自己Ruby和我试图解决的某个问题我注意到很多人在他们的代码中使用=~/\\ I'm not really sure how they work and would just like an explanation. 我不确定它们是如何工作的,只是想解释一下。 For example I was looking at someones code for this Pig Latin translator and this is the first time I'm seeing these being used. 例如,我正在查看这个Pig Latin翻译器的某些代码,这是我第一次看到这些被使用。

def piglatin(word)
   if word =~ (/\A[aeiou]/i)
      word = word + 'ay'
   elsif word =~ (/\A[^aeiou]/i)
      match = /\A[^aeiou]/i.match(word)
      word = match.post_match + match.to_s + 'ay'
   end
word
end

I'm just confused about the /\\ slashes and the =~ 我只是对/\\ slashes和=~感到困惑

=~ is known as the "match operator" and can be used to match a string against a regular expression. =~被称为“匹配运算符”,可用于将字符串与正则表达式进行匹配。

The /\\ is actually part of two separate things. /\\实际上是两个独立事物的一部分。 / denotes the start of a regular expression and \\A is known as an "anchor" and is saying "match from the beginning of the string." /表示正则表达式的开头, \\A称为“锚点”,表示“从字符串的开头匹配”。

edit: This is a link to the documentation that should help you understand more code like you posted. 编辑: 这是一个文档的链接,可以帮助您了解更多像您发布的代码。

thank you to Wayne Conrad for a correction on '/\\' 谢谢Wayne Conrad对'/ \\'进行更正

=~ is Ruby's pattern-matching operator. =~是Ruby的模式匹配运算符。

It matches a regular expression on the left to a string on the right. 它将左侧的正则表达式与右侧的字符串相匹配。

If a match is found, the index of first match in string is returned. 如果找到匹配项,则返回字符串中第一个匹配项的索引。 If the string cannot be found, nil will be returned. 如果找不到该字符串,则返回nil。

/abc/ =~ "abcdef"

In this case, the expression returns 0, because that is the index of the first match of "abc" in the string. 在这种情况下,表达式返回0,因为这是字符串中“abc”的第一个匹配的索引。

/xyz/ =~ "abcdef"

returns nil because "xyz" cannot be found anywhere in the string. 返回nil,因为在字符串中的任何地方都找不到“xyz”。

As for /\\ : 至于/\\

/     Defines the start and end of a regular expression
\     References a regular expression

For example: 例如:

\d => Matches all digits

The equal-tilde operator in Ruby is the “match” operator. Ruby中的等号运算符是“匹配”运算符。 It take an regular expression on the left hand side and the string to match on the right hand side. 它在左侧采用正则表达式,在右侧采用匹配的字符串。 The expression: 表达方式:

/or/ =~ “Hello World”

will return 7 because a match is found on index 7 of the string. 将返回7,因为在字符串的索引7上找到匹配项。 The index starts at 0. 索引从0开始。

The expression: 表达方式:

/abc/ =~ “Hello World”

will return nil because there is no match. 将返回零,因为没有匹配。

The use of /\\A and =~ aside, that code is not written well, so don't emulate it. 使用/\\A=~除此之外,该代码写得不好,所以不要模仿它。 This is a bit more Ruby-like: 这有点像Ruby一样:

def piglatin(word)
  if word[/\A[aeiou]/i]
    word + 'ay'
  else
    word[1..-1] + word[0] + 'ay'
  end
end

piglatin('apple')   # => "appleay"
piglatin('banana')  # => "ananabay"

For this purpose, ^ would have worked as well as \\A as they're both "beginning of..." anchors. 为了这个目的, ^将和\\A一样工作,因为它们都是“......的开头”。 These are from the Anchors definitions : 这些来自Anchors定义

  • ^ - Matches beginning of line ^ - 匹配行首
  • \\A - Matches beginning of string. \\A - 匹配字符串的开头。

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

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