2

Matching S01E01 in Ruby

with

/S\d+?E\d+?/ix

which works, however, S01 E01 does not. I thought the /x should ignore white spaces?

2 Answers 2

10

/x ignores whitespace inside your regex, not in the text you're matching.

You're looking for

/S\d+?\s*E\d+?/i
Sign up to request clarification or add additional context in comments.

Comments

3

The x option ignores whitespace in the regex itself, which allows you to better format your regexes for reading without modifying their meaning. You could write:

irb(main):008:0> r = /
irb(main):009:0/ S
irb(main):010:0/ d+?
irb(main):011:0/ E
irb(main):012:0/ d+?
irb(main):013:0/ /ix

To get an regex with the same meaning as your example.

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.