[英]extract numbers from string in ruby regex
I have a string as 我有一个字符串
Teen Mom 2 (Season 5) |
青少年妈妈2(第5季)| Ep.
EP。 7 |
7 | These Are The Days |
这些日子 MTV
MTV
I need to extract only Season number and Episode number from this. 我只需要从中提取季号和剧集号。 ie.
即。 5 and 7.
5和7。
Please help. 请帮忙。
Use string.scan
function like below. 使用如下所示的
string.scan
函数。
> "Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV".scan(/(?<=Season )\d+|(?<=Ep\. )\d+/)
=> ["5", "7"]
(?<=Season )
Positive look-behind asserts that the match must be preceded by Season
string. (?<=Season )
正向后置断言,比赛必须以Season
字符串开头。 \\d+
Match one or more digits. \\d+
匹配一个或多个数字。 Here is a method which generates a named match: 这是一种生成命名匹配的方法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.