daemonza
daemonza

Reputation: 527

Ruby regex not ignoring whitespace

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?

Upvotes: 2

Views: 3253

Answers (2)

beerbajay
beerbajay

Reputation: 20270

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.

Upvotes: 3

Dogbert
Dogbert

Reputation: 222118

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

You're looking for

/S\d+?\s*E\d+?/i

Upvotes: 10

Related Questions