我尝试了以下方法:
(\([^0-9]*\d+[^0-9]*\))
对此:
Sales rank: 398 (was 644)
但我明白了:
(was 644)
我只需要644
我怎样才能修复我的正则表达式来实现这一点?
尝试以下操作:
>> 'Sales rank: 398 (was 644)'[/\d+(?=\))/]
=> "644"
(?=\))
用于断言模式后跟关闭括号。(积极前瞻)
选择
>> 'Sales rank: 398 (was 644)'.scan(/\d+/)
=> ["398", "644"]
>> 'Sales rank: 398 (was 644)'.scan(/\d+/).last
=> "644"
您也可以将其regex
与@falsetru 的答案一起使用
(?<=was )\d\d\d
使用扫描...
>> 'Sales rank: 398 (was 644)'.scan(/\d+/)
=> ["398", "644"]
>> 'Sales rank: 398 (was 644)'.scan(/\d+/).last
=> "644"
谢谢