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
从ruby正则表达式中的字符串中提取数字_Ruby_Regex - Fatal编程技术网

从ruby正则表达式中的字符串中提取数字

从ruby正则表达式中的字符串中提取数字,ruby,regex,Ruby,Regex,我有一根绳子 青少年妈妈2(第五季)| Ep。7 |现在是| MTV的日子 我只需要从中提取季号和集号。即5号和7号 请提供帮助。使用字符串。扫描功能如下所示 > "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV".scan(/(?<=Season )\d+|(?<=Ep\. )\d+/) => ["5", "7"] “青少年妈妈2(第五季)| Ep.7 |这是MTV的日子”。扫描(/(?使用字符串。扫描

我有一根绳子

青少年妈妈2(第五季)| Ep。7 |现在是| MTV的日子

我只需要从中提取季号和集号。即5号和7号


请提供帮助。

使用
字符串。扫描功能如下所示

> "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV".scan(/(?<=Season )\d+|(?<=Ep\. )\d+/)
=> ["5", "7"]

“青少年妈妈2(第五季)| Ep.7 |这是MTV的日子”。扫描(/(?使用
字符串。扫描
功能如下

> "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV".scan(/(?<=Season )\d+|(?<=Ep\. )\d+/)
=> ["5", "7"]

>“青少年妈妈2(第五季)| Ep.7 |这是几天| MTV”。扫描(/(?以下是生成命名匹配的方法:

txt = "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV"
match = /Season (?<season>\d+).*Ep\. (?<ep>\d+)/.match txt

match['season']
# => 5
match['ep']
# => 7
txt=“青少年妈妈2(第五季)|第七集|这是MTV的日子”
match=/seasure(?\d+).*Ep\(?\d+)/.match-txt
比赛[季节]
# => 5
匹配['ep']
# => 7

以下是一个生成命名匹配的方法:

txt = "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV"
match = /Season (?<season>\d+).*Ep\. (?<ep>\d+)/.match txt

match['season']
# => 5
match['ep']
# => 7
txt=“青少年妈妈2(第五季)|第七集|这是MTV的日子”
match=/seasure(?\d+).*Ep\(?\d+)/.match-txt
比赛[季节]
# => 5
匹配['ep']
# => 7

(?:Season | Ep\)\s+\K\d+
回答好,很好\Komment。或
(?:Season | Ep\)\s+\K\d+
回答好,很好\Komment。