如何在字符串中匹配字符串,然后将其提取到变量中

时间:2021-09-07 06:53:15

I have come across a problem that I cannot see to solve. I have extracted a line from a web page into a variable. lets say for argument sake this is:

我遇到了一个我看不到解决的问题。我从网页中提取了一行作为变量。为了讨论这个问题

rhyme = "three blind mice Version 6.0"

押韵= "三只瞎老鼠版本6.0"

and I want to be able to first of all locate the version number within this string (6.0) and secondly extract this number into another seperate variable - (I want to specifically extract no more than "6.0")

我想首先找到这个字符串中的版本号(6.0)然后将这个数字提取到另一个独立的变量中(我想要提取不超过6.0)

I hope I have clarified this enough, if not please ask me anything you need to know and I will get back to you asap.

我希望我已经讲得足够清楚了,如果没有,请问我任何你需要知道的事情,我会尽快回复你。

3 个解决方案

#1


5  

First you need to decide what the pattern for a version number should be. One possibility would be \d+(\.\d+)*$ (a number followed by zero or more (dot followed by a number) at the end of the string).

首先,您需要确定版本号的模式应该是什么。一种可能是\d+(\. d+)*$(一个数字后面跟着零或更多(点后面跟着一个数字)在字符串的末尾)。

Then you can use String#[] to get the substring that matches the pattern:

然后可以使用String#[]获取匹配模式的子字符串:

rhyme[ /\d+(\.\d+)*$/ ] #=> "6.0"

#2


2  

You need to use regular expressions. I would use rhyme.scan(/(\d+\.\d+)/) since it can return an array if multiple matches occur. It can also take a block so that you can add range checks or other checks to ensure the right one is captured.

你需要使用正则表达式。我将使用rhy. scan(/(\d+\.\d+)/),因为如果发生多个匹配,它可以返回一个数组。它还可以使用一个块,以便您可以添加范围检查或其他检查,以确保捕获正确的范围检查。

version = "0.0"
rhyme = "three blind mice Version 6.0"
rhyme.scan(/(\d+\.\d+)/){|x| version = x[0] if x[0].to_f < 99}
p version

If the input can be trusted to yield only one match or if you always are going to use the first match you can just use the solution in this answer.

如果输入只能产生一个匹配,或者总是使用第一个匹配,那么可以在这个答案中使用这个解决方案。

Edit: So after our discussion just go with that answer.

编辑:所以在我们的讨论之后,请给出答案。

#3


0  

if rhyme =~ /(\d\.\d)/
    version = $1
end

The regexp matches a digit, followed by a period, followed by another digit. The parenthesis captures its contents. Since it is the first pair of parenthesis, it is mapped to $1.

regexp匹配一个数字,后跟一个句号,然后是另一个数字。括号抓住了它的内容。因为它是第一对括号,所以它被映射为$1。

#1


5  

First you need to decide what the pattern for a version number should be. One possibility would be \d+(\.\d+)*$ (a number followed by zero or more (dot followed by a number) at the end of the string).

首先,您需要确定版本号的模式应该是什么。一种可能是\d+(\. d+)*$(一个数字后面跟着零或更多(点后面跟着一个数字)在字符串的末尾)。

Then you can use String#[] to get the substring that matches the pattern:

然后可以使用String#[]获取匹配模式的子字符串:

rhyme[ /\d+(\.\d+)*$/ ] #=> "6.0"

#2


2  

You need to use regular expressions. I would use rhyme.scan(/(\d+\.\d+)/) since it can return an array if multiple matches occur. It can also take a block so that you can add range checks or other checks to ensure the right one is captured.

你需要使用正则表达式。我将使用rhy. scan(/(\d+\.\d+)/),因为如果发生多个匹配,它可以返回一个数组。它还可以使用一个块,以便您可以添加范围检查或其他检查,以确保捕获正确的范围检查。

version = "0.0"
rhyme = "three blind mice Version 6.0"
rhyme.scan(/(\d+\.\d+)/){|x| version = x[0] if x[0].to_f < 99}
p version

If the input can be trusted to yield only one match or if you always are going to use the first match you can just use the solution in this answer.

如果输入只能产生一个匹配,或者总是使用第一个匹配,那么可以在这个答案中使用这个解决方案。

Edit: So after our discussion just go with that answer.

编辑:所以在我们的讨论之后,请给出答案。

#3


0  

if rhyme =~ /(\d\.\d)/
    version = $1
end

The regexp matches a digit, followed by a period, followed by another digit. The parenthesis captures its contents. Since it is the first pair of parenthesis, it is mapped to $1.

regexp匹配一个数字,后跟一个句号,然后是另一个数字。括号抓住了它的内容。因为它是第一对括号,所以它被映射为$1。