rbp-book icon indicating copy to clipboard operation
rbp-book copied to clipboard

bug in Regexp in Chapter 4

Open aidancully opened this issue 14 years ago • 0 comments

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.

aidancully avatar Nov 30 '10 12:11 aidancully