tkersey/isbn

Fix regex in ISBN.from_string

ascastro opened this issue · 0 comments

Let's say we have the following ISBN:

isbn = "9-780230-104143"
=> "9-780230-104143"

Checking if that is a valid ISBN, returns true:

ISBN.valid?(isbn)
=> true

However, if we try to extract it from a string, it throws an exception:

ISBN.thirteen(ISBN.from_string(isbn))
=> ISBN::InvalidSourceString: ISBN::InvalidSourceString

The reason for that is because the regex requires the - to be after 978.
Since spaces and hyphens in the ISBN do not matter, is it possible to fix the regex in from_string so that the positioning of the hyphen doesn't matter?
Something like:

/(?:ISBN[- ]*13|ISBN[- ]*10|)\s*((?:(?:9[\s-]*7[\s-]*[89])?[ -]?(?:[0-9][ -]*){9})[ -]*(?:[0-9xX]))/

Thank you!