Reputation: 11
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
Reputation: 5596
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
How it works:
ISBN: # (ISBN:)
\s* # Optional Whitespace
(\d+) # Capture Digits
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)
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
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+
Upvotes: 2
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