Reputation: 3913
Consider the following Ruby code:
/(?<!big )dog/.match('I have a big dog.') # => nil
Now, I'm going to enable free-spacing mode:
/(?x)(?<!big )dog/.match('I have a big dog.') # => #<MatchData "dog">
Why does this happen, and how can I enable free-spacing mode without breaking my negative lookbehinds?
Upvotes: 4
Views: 51
Reputation: 122383
/(?x)(?<!big )dog/.match('I have a big dog.')
# ^
Note that you have a whitespace after big
. Since it's the extended mode, the whitespace is ignored.
You have some options:
\s
or \p{Space}
.\
, i.e. a space preceded by a
backslash.[ ]
.For example:
/(?x)(?<!big\s)dog/.match('I have a big dog.')
# => nil
Upvotes: 3