rbp-book
rbp-book copied to clipboard
bug in Regexp in Chapter 4
Chapter 4 / Regular Expressions / Don't Work Too Hard introduces the section with the following code:
["James Gray", "James gray", "james gray", "james Gray"].all? { |e| ?> e.match(/James|james Gray|gray/) }
The regex in this example (/James|james Gray|gray/
) will match any string containing "James", or "james Gray", or "gray", which is probably not what you intended. That is,
>> "gray".match(/James|james Gray|gray/) && true => true >> "Gray".match(/James|james Gray|gray/) && true => nil
Suggest either /(James|james) (Gray|gray)/
or /(?:James|james) (?:Gray|gray)
, or note that finding the bug is an exercise for the reader.