我有一个字符串
青少年妈妈2(第5季)| EP。 7 |这些是天| MTV
我需要从中提取季节编号和剧集编号。即。 5和7.
请帮忙。
答案 0 :(得分:4)
使用string.scan
功能,如下所示。
> "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV".scan(/(?<=Season )\d+|(?<=Ep\. )\d+/)
=> ["5", "7"]
(?<=Season )
肯定的后卫声称匹配必须以Season
字符串开头。\d+
匹配一个或多个数字。答案 1 :(得分:1)
这是一个生成命名匹配的方法:
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