wrangly
wrangly

Reputation: 37

Get ISBN number with regex

ISBN numbers come with random dash positions

978-618-81543-7-7
9786-18-81-5437-7
97-86-18-81-5437-7

How could i get them every time without knowing dash positions?

Upvotes: 1

Views: 337

Answers (2)

Eric Duminil
Eric Duminil

Reputation: 54223

Just delete every - with your language of choice.

With Ruby :

"978-618-81543-7-7".delete('-')
#=> "9786188154377"

If you really want to use a regex :

"978-618-81543-7-7".gsub(/-/,'')

If you have multiple lines with isbns :

isbns = "978-618-81543-7-7
9786-18-81-5437-7
97-86-18-81-5437-7"

p isbns.scan(/\b[-\d]+\b/).map{|number_and_dash| number_and_dash.delete('-')}
#=> ["9786188154377", "9786188154377", "9786188154377"]

Upvotes: 3

kabanus
kabanus

Reputation: 25895

Pretty easy use of regex, google it to learn more. In Python:

import re
nums=re.findall('\d+',isbnstring)

This will give a list of the numbers. To join them to a string:

isbn=''.join(nums)

As per comments below, if you're working a file you could work it line by line:

with open(isbnfile) as desc:
    for isbnstring in desc:
        #Do the above and more.

As one example. There are a ton of ways to do this. I just realized from the command line sed is a good choice as well:

sed 's/-//g' isbnfile > newisbnfile

Upvotes: 1

Related Questions