0

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.

2 Answers 2

5

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.
Sign up to request clarification or add additional context in comments.

2 Comments

or (?:Season|Ep\.)\s+\K\d+
Good answer, great \Komment.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.