Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 可以包含多个数字的字符串-如何提取最长的数字?_Ruby_Regex - Fatal编程技术网

Ruby 可以包含多个数字的字符串-如何提取最长的数字?

Ruby 可以包含多个数字的字符串-如何提取最长的数字?,ruby,regex,Ruby,Regex,我有一根绳子 包含至少一个数字 可以包含多个数字 例如: https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384 https://www.facebook.com/username_13/posts/101505775425651120 https://www.facebook.com/username/posts/101505775425699820 我需要一种从字符串中提取最长数字的方

我有一根绳子

  • 包含至少一个数字
  • 可以包含多个数字
例如:

https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384
https://www.facebook.com/username_13/posts/101505775425651120
https://www.facebook.com/username/posts/101505775425699820
我需要一种从字符串中提取最长数字的方法。因此,对于上面的3个字符串,它将提取

53199604568
101505775425651120
101505775425699820
如何执行此操作?

解析列表(以获取int数组),然后使用Max函数。语法为array.Max。

解析列表(获取int数组),然后使用Max函数。语法为array.Max。

#首先获取行
#get the lines first
text = <<ENDTEXT
https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384
https://www.facebook.com/username_13/posts/101505775425651120
https://www.facebook.com/username/posts/101505775425699820
ENDTEXT
lines = text.split("\n")

#this bit is the actual answer to your question
lines.collect{|line| line.scan(/\d+/).sort_by(&:length).last}
text=
#首先获取行
text=
s=”https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384"
s、 扫描(/\d+/).max{a,b | a.length b.length}.to|i
s=”https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384"
s、 扫描(/\d+/).max{a,b | a.length b.length}.to|i

1.9有一个
max\u by
方法,所以你可以做
line.scan(/\d+/).max\u by(&:length)
.1.9也有一个.lines方法,所以你最终会得到类似
text.lines.map{line | line.scan(/\d+/).max\u by(&:to\u i)}
1.9有一个
max\u by
方法,所以你可以做
line.scan(/\d+)&:max/)
.1.9还有一个.lines方法,因此您最终会得到类似于
text.lines.map{line | line.scan(/\d+/).max_by(&:to_i)}
的东西。请注意,如果您仍然要对字符串进行排序,那么您可以简单地根据整数值进行排序-更长的字符串意味着更大的整数。请注意,如果您仍然要对字符串进行排序,然后,您可以简单地根据整数值进行排序——更长的字符串意味着更大的整数。
s = "https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384"
s.scan(/\d+/).max{|a,b| a.length <=> b.length}.to_i