I have a string as
Teen Mom 2 (Season 5) | Ep. 7 | These Are The Days | MTV
I need to extract only Season number and Episode number from this. ie. 5 and 7.
Please help.
Use string.scan
function like below.
> "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. \\d+
Match one or more digits. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.