Skip to content

bug in Regexp in Chapter 4 #8

@aidancully

Description

@aidancully

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions