A friend of mine is trying to explain to me the answer to this problem:
我的一个朋友试图向我解释这个问题的答案:
Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4.
定义一个方法binary_multiple_of_4?
However, his example he gave is this:
然而,他举的例子是:
if (s) == "0"
return true
end
if /^[01]*(00)$/.match(s) #|| /^0$/.match(s)
return true
else
return false
end
It works, because the software we use says there were no errors, but I don't understand why, or what /^ means, and how it's used.
说它,因为我们使用的软件没有错误,但是我不明白为什么,或者/ ^意味着什么,它是如何使用的。
If you could also explain the /^0$/.match(s), that would be great too.
如果你也可以解释/ ^ 0美元/ .match(s),这将是伟大的。
Thanks!
谢谢!
1 个解决方案
#1
3
what he is doing is using regular expressions, see: http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
他所做的是使用正则表达式,参见:http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm !
To break it down, there is a pattern that is matched inside the slashes /pattern/
and every character means something. ^
means start of the line [01]
means match a 0
or a 1
, *
means match the previous thing ([01]
) zero or more times, and (00)
means match 00
, and $
means match the end of the line.
为了打破它,在斜杠/模式/和每个字符中都有一个匹配的模式。^意味着开始行(01)匹配一个0或1,*意味着匹配之前的事情([1])零个或多个时期,美元(00)意味着匹配00,意味着结束比赛。
If you want to know what /^0$/ matches, you should definitely try to figure it out based on the information in my post or the link I provided. Here's the answer though (hover to view):
如果你想知道什么/ ^ 0美元/比赛,你应该试着弄明白根据我提供的信息在我的帖子或者链接。以下是答案(鼠标悬停查看):
It matches the beginning of the line, zero, the end of a line.
它匹配线的开始,零,线的结束。
#1
3
what he is doing is using regular expressions, see: http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
他所做的是使用正则表达式,参见:http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm !
To break it down, there is a pattern that is matched inside the slashes /pattern/
and every character means something. ^
means start of the line [01]
means match a 0
or a 1
, *
means match the previous thing ([01]
) zero or more times, and (00)
means match 00
, and $
means match the end of the line.
为了打破它,在斜杠/模式/和每个字符中都有一个匹配的模式。^意味着开始行(01)匹配一个0或1,*意味着匹配之前的事情([1])零个或多个时期,美元(00)意味着匹配00,意味着结束比赛。
If you want to know what /^0$/ matches, you should definitely try to figure it out based on the information in my post or the link I provided. Here's the answer though (hover to view):
如果你想知道什么/ ^ 0美元/比赛,你应该试着弄明白根据我提供的信息在我的帖子或者链接。以下是答案(鼠标悬停查看):
It matches the beginning of the line, zero, the end of a line.
它匹配线的开始,零,线的结束。