Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 从以下字符串中获得51的最佳方法是什么_Ruby_Regex_Url - Fatal编程技术网

Ruby 从以下字符串中获得51的最佳方法是什么

Ruby 从以下字符串中获得51的最佳方法是什么,ruby,regex,url,Ruby,Regex,Url,从ruby中的以下字符串中获取“51”的最佳方法是什么: "<https://api.example.com/users/lgs/api?page=2>; rel=\"next\", <https://api.example.com/users/lgs/api?page=51>; rel=\"last\"" “rel=”下一个“,;rel=”最后一个” 提前谢谢 卢卡如果您知道该字符串中只有两个数字,那么这就足够了: str = '"<https://api.e

从ruby中的以下字符串中获取“51”的最佳方法是什么:

"<https://api.example.com/users/lgs/api?page=2>; rel=\"next\", <https://api.example.com/users/lgs/api?page=51>; rel=\"last\""
“rel=”下一个“,;rel=”最后一个”
提前谢谢
卢卡

如果您知道该字符串中只有两个数字,那么这就足够了:

str = '"<https://api.example.com/users/lgs/api?page=2>; rel=\"next\", <https://api.example.com/users/lgs/api?page=51>; rel=\"last\""'
p str.scan(/\d+/).last #=> "51"
str='“rel=\“next\”,;rel=\“last\”
p str.scan(/\d+/).last#=>“51”

如果没有,则提供更多细节,使正则表达式更精确。如果您需要答案作为数字,也可以将
添加到\u i

您可以通过以下方式使用regexp:

res=str.match/.page=(\d+)/

通过这种方式,您将“捕获”(“和”)之间的所有数字”(在最后一个标记中),并且您的结果将存储在

第一项决议 (或仅以$1变量表示)


你知道字符串是如何编码的吗?你能再举一些例子吗,包括一些边缘情况吗?你对输入有多大的控制权?你能保证哪些部分永远是相同的,哪些部分可以改变?(为什么)你用你的答案标记了它吗?@sehe…好的,现在我明白了()