Marcelo Mariussi
Marcelo Mariussi

Reputation: 11

Regex to extract number from "ISBN: 9783295359"

I've got data formatted like this:

ISBN: 9783295359

How can I use a regex to isolate only the number 9783295359? I only need the number, so I need to exclude the "ISBN: ".

Upvotes: 0

Views: 93

Answers (2)

Kaspar Lee
Kaspar Lee

Reputation: 5596

Normal Version

Try this RegEx:

ISBN:\s*(\d+)

The data is stored in the first capture group. To make it slightly safer, add a $ to the end

Live Demo on RegExr

How it works:

ISBN:    # (ISBN:)
\s*      # Optional Whitespace
(\d+)    # Capture Digits

Simplest Version

Note that you could get it as short as (\d+) (since the only set of digits is the number you want to extract), or slightly safer, (\d+)$ (to make sure the number appears at the end of the string)

Live Demo on RegExr


Safest Version

The safest version would be this:

ISBN:\s*(\d{10})$

The {10} specifies 10 digits, i.e it is equivalent to \d\d\d\d\d\d\d\d\d\d

Live Demo on RegExr


If you cannot get data from one capture group for whatever reason, or just want shorter code, you can use a Positive Lookbehind to not select the ISBN: part. You could need to change the ISBN:\s* part to:

(?<=ISBN:\s)

Note that this will only allow one whitespace between the : and ISBN Number. Also, this method only works if you are using PCRE (Pearl Compatible Regular Expressions). You can also remove the () around \d+

Live Demo on Regex101

Upvotes: 2

Saleem
Saleem

Reputation: 8978

You an extract this using simple regex. See PCRE example below.

(?<=ISBN:\s)(\d+)

This regex will exclude ISBN: and just capture numeric part.

See demo at https://regex101.com/r/zK1uM9/1

Upvotes: 0

Related Questions