re = /(\d{1,2})[^\d]{1,2}(\d{1,2})(?:[^\d]{1,2}(\d{1,2}))?.*/im
str = 'S01E02E03
01x01 Lorem ipsum.avi Result: 01, 01
01x01-02 Lorem ipsum.avi Result: 01, 02 Expected: 01, 01, 02
1x1 Lorem ipsum.avi Result: 1, 1
1x1-2 Lorem ipsum.avi Result: 1, 2 Expected: 1, 1, 2
S01E01.HDTV.x264-LOL.mp4 Result: 01, 01
S01E01E02.HDTV.x264-LOL.mp4 Result: 01, 02 Expected: 01, 01, 02
S01E01-02.HDTV.x264-LOL.mp4 Result: 01, 02 Expected: 01, 01, 02
S01E01-E02.HDTV.x264-LOL.mp4 Result: 01, 02 Expected: 01, 01, 02
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html