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 on rails 从字符串中提取数字_Ruby On Rails_Regex - Fatal编程技术网

Ruby on rails 从字符串中提取数字

Ruby on rails 从字符串中提取数字,ruby-on-rails,regex,Ruby On Rails,Regex,假设我有这样一个字符串: "1 hour 7 mins" hours = duration.match(/[\d]* hour/).to_s.gsub(/[^\d]/, '') minutes = duration.match(/[\d]* mins/).to_s.gsub(/[^\d]/, '') 我需要提取小时数(1)和分钟数(7)。问题是小时或分钟可以是nill,因此在这种情况下,字符串将是1小时ot,而不是7分钟 我对正则表达式最感兴趣。我已经安装并运行了此代码 result

假设我有这样一个字符串:

"1 hour 7 mins"
hours = duration.match(/[\d]* hour/).to_s.gsub(/[^\d]/, '')
minutes = duration.match(/[\d]* mins/).to_s.gsub(/[^\d]/, '')
我需要提取
小时数
(1)和
分钟数
(7)。问题是小时或分钟可以是
nill
,因此在这种情况下,字符串将是
1小时
ot,而不是
7分钟
我对正则表达式最感兴趣。我已经安装并运行了此代码

    result = duration.gsub(/[^\d]/, '') 

   result[0]!= nil ? hour=result[0] : hour=0 

   result[1]!=nil ? mins=result[1] : mins=0 
问题是,当我只有
5分钟时,它给了我5分钟,而我不知道它是
mins
还是
hour
那我该怎么做呢?

你可以这样做:

a = duration[/(\d*)(\s*hour)?s?\s*(\d*)(\s*min)?s?/][0]
if a.include?("hour")
  hour = a[0]
  min = a[2]
else
  min = a[0]
end
改进,这就是我想要的:

capture = duration.match(/^((\d*) ?hour)?s? ?((\d*) ?min)?s?/)
hour = capture[2]
min = capture[4]
您可以在此处尝试正则表达式: 您可以这样做:

a = duration[/(\d*)(\s*hour)?s?\s*(\d*)(\s*min)?s?/][0]
if a.include?("hour")
  hour = a[0]
  min = a[2]
else
  min = a[0]
end
改进,这就是我想要的:

capture = duration.match(/^((\d*) ?hour)?s? ?((\d*) ?min)?s?/)
hour = capture[2]
min = capture[4]
您可以在此处尝试正则表达式:

你怎么看待这样的事情:

"1 hour 7 mins"
hours = duration.match(/[\d]* hour/).to_s.gsub(/[^\d]/, '')
minutes = duration.match(/[\d]* mins/).to_s.gsub(/[^\d]/, '')

你怎么看待这样的事情:

"1 hour 7 mins"
hours = duration.match(/[\d]* hour/).to_s.gsub(/[^\d]/, '')
minutes = duration.match(/[\d]* mins/).to_s.gsub(/[^\d]/, '')

我忍不住要打高尔夫:

你可以做:

hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
说明:

匹配数字,然后是“h”,然后是任何非数字,然后是数字,然后是“m”。然后获取匹配数据并执行.to_i(在ruby中,如果以数字开头,则使用该数字)。然后,它将第一场比赛和第三场比赛分别分配给小时和分钟:

输出:

2.2.1 :001 > duration = "5 hours 26 min"
 => "5 hours 26 min" 
2.2.1 :002 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [5, 0, 26] 
2.2.1 :003 > hours
 => 5 
2.2.1 :004 > mins
 => 26 
2.2.1 :005 > duration = "5 hours"
 => "5 hours" 
2.2.1 :006 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [5, 0, 0] 
2.2.1 :007 > duration = "54 mins"
 => "54 mins" 
2.2.1 :008 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [0, 0, 54] 
2.2.1 :009 > 

我忍不住要打高尔夫:

你可以做:

hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
说明:

匹配数字,然后是“h”,然后是任何非数字,然后是数字,然后是“m”。然后获取匹配数据并执行.to_i(在ruby中,如果以数字开头,则使用该数字)。然后,它将第一场比赛和第三场比赛分别分配给小时和分钟:

输出:

2.2.1 :001 > duration = "5 hours 26 min"
 => "5 hours 26 min" 
2.2.1 :002 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [5, 0, 26] 
2.2.1 :003 > hours
 => 5 
2.2.1 :004 > mins
 => 26 
2.2.1 :005 > duration = "5 hours"
 => "5 hours" 
2.2.1 :006 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [5, 0, 0] 
2.2.1 :007 > duration = "54 mins"
 => "54 mins" 
2.2.1 :008 > hours,_,mins = (duration.match /^([\d]* h)?([^\d]*)?([\d]* m)?/)[1..3].map(&:to_i)
 => [0, 0, 54] 
2.2.1 :009 > 

你不能这样做你能给我一个解决方案吗?你不能这样做你能给我一个解决方案吗?为什么这么复杂的正则表达式?嗯,我不知道,你有什么改进建议吗?哦,我知道我做了什么。我的错,我想要更简单的东西,但我被正则表达式的谜题困住了。为什么要这么复杂的正则表达式?嗯,我不知道,你有什么改进建议吗?哦,我知道我做了什么。我的缺点是,我想要更简单的东西,但我被正则表达式的难题困住了。你的方法很好,但我认为你应该放:/(\d*)小时?/和/(\d*)分钟?/即使是单数和复数,它也能正确提取。@Oxynum你有一个正确的观点,但没有必要添加可选的
s?
。请记住正则表达式匹配字符串中的任何位置,因此
/\d+min/
也应该可以。您的方式很好,但我认为您应该放:/(\d*)小时?/和/(\d*)分钟?/即使使用单数和复数,它也可以正确提取。@Oxynum您有一个有效点,但无需添加可选的
s?
。记住正则表达式匹配字符串中的任何位置,因此
/\d+min/
也应该可以工作。