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

时间:2022-07-25 16:40:21

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. 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.

我一直在教自己Ruby和我试图解决的某个问题我注意到很多人在他们的代码中使用=〜和/ \。我不确定它们是如何工作的,只是想解释一下。例如,我正在查看这个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和=〜感到困惑

4 个解决方案

#1


25  

=~ 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对'/ \'的更正

#2


6  

=~ 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

#3


1  

The equal-tilde operator in Ruby is the “match” operator. It take an regular expression on the left hand side and the string to match on the right hand side. The expression:

Ruby中的equal-tilde运算符是“匹配”运算符。它在左侧采用正则表达式,在右侧采用匹配的字符串。表达方式:

/or/ =~ “Hello World”

will return 7 because a match is found on index 7 of the string. The index starts at 0.

将返回7,因为在字符串的索引7上找到匹配项。索引从0开始。

The expression:

/abc/ =~ “Hello World”

will return nil because there is no match.

将返回零,因为没有匹配。

#4


0  

The use of /\A and =~ aside, that code is not written well, so don't emulate it. This is a bit more Ruby-like:

使用/ \ A和=〜除此之外,该代码编写得不好,所以不要模拟它。这有点像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. These are from the Anchors definitions:

为了这个目的,^将和\ A一样工作,因为它们都是“......的开头”。这些来自Anchors的定义:

  • ^ - Matches beginning of line
  • ^ - 匹配行首

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

#1


25  

=~ 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对'/ \'的更正

#2


6  

=~ 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

#3


1  

The equal-tilde operator in Ruby is the “match” operator. It take an regular expression on the left hand side and the string to match on the right hand side. The expression:

Ruby中的equal-tilde运算符是“匹配”运算符。它在左侧采用正则表达式,在右侧采用匹配的字符串。表达方式:

/or/ =~ “Hello World”

will return 7 because a match is found on index 7 of the string. The index starts at 0.

将返回7,因为在字符串的索引7上找到匹配项。索引从0开始。

The expression:

/abc/ =~ “Hello World”

will return nil because there is no match.

将返回零,因为没有匹配。

#4


0  

The use of /\A and =~ aside, that code is not written well, so don't emulate it. This is a bit more Ruby-like:

使用/ \ A和=〜除此之外,该代码编写得不好,所以不要模拟它。这有点像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. These are from the Anchors definitions:

为了这个目的,^将和\ A一样工作,因为它们都是“......的开头”。这些来自Anchors的定义:

  • ^ - Matches beginning of line
  • ^ - 匹配行首

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