使用正则表达式从字符串模式中检索数字 [英] Retrieve number from the string pattern using regular expression
问题描述
我有一个字符串Search result:16143 Results found",我需要从中检索16143.
I have a string "Search result:16143 Results found", and I need to retrieve 16143 out of it.
我正在用 ruby 编码,我知道使用 RegEx 来获得它会很干净(反对基于分隔符拆分字符串)
I am coding in ruby and I know this would be clean to get it using RegEx (as oppose to splitting string based on delimiters)
如何在 ruby 中从这个字符串中检索数字?
How would I retrieve the number from this string in ruby?
推荐答案
我不确定 Ruby 中的语法,但正则表达式将是 "(\d+)" 表示大小为 1 或更大的数字字符串.您可以在这里尝试:http://www.rubular.com/
I'm not sure on the syntax in Ruby, but the regular expression would be "(\d+)" meaning a string of digits of size 1 or more. You can try it out here: http://www.rubular.com/
更新:我相信语法是/(\d+)/.match(your_string)
Updated: I believe the syntax is /(\d+)/.match(your_string)
这篇关于使用正则表达式从字符串模式中检索数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!