-1

I have a file in which valid ISBNs always appear in the context of

isbn = {<ISBN>}

but often lack the dashes that are generally used for formatting them. I would like to insert these dashes using grep in BBEdit. For example I'd like to replace

isbn = {0226104036}

with

isbn = {0-226-10403-6}

but in order to do so I need to break up the string of digits, which requires ISBN encoding knowledge and grep skills I don't quite have.

I get as far as searching for

isbn = {([0-5]|7|60[0-9]|61[0-7]|8[0-9]|9[1-4]|9[5-8][0-9]|992[7-9]|99[3-8][0-9]|9990[1-9]|999[1-5][1-9]|9996[1-7])([0-9]+)([0-9]|X)}

and replace with

isbn = {\1-\2-\3}

which, in the above example, only gets me as far as

isbn = {0-22610403-6}

and might even put the first dash in the wrong place in some instances.

Note that I can assume that the ISBNs are all ISBN-10. Also note that I'm doing this in BBEdit, which has some grep syntax quirks. The related solutions I've found here either fail in BBEdit, or I can't adapt them to this particular scenario.

orome
  • 45,163
  • 57
  • 202
  • 418
  • What happens when you use your current regex? Can you give some examples of successful / unsuccessful results? – octern Mar 27 '12 at 21:04
  • @octern: It always works, but (1) I'm not sure it's parsing out the first pattern (the "group") correctly (i.e. my simplistic interpretation of the ISBN-10 spec may be eating some digits in the first pattern that really belong in the second) and (2) the second pattern should be split into two (the "publisher" and "title"). – orome Mar 27 '12 at 21:21

1 Answers1

-1

One thing I see right off is that { and } are special characters in regex, so you need to escape them (put a backslash before the character). This only applies to the text in Search, not in Replace. This may not be the sole cause of your problem but it's probably necessary.

Also, did you try a web search for "ISBN regular expressions"? For common patterns like this, there's usually a known and validated solution already. Here's one that might help. I don't have the knowledge to check it myself, but if this doesn't work there are lots of other proposed solutions out there.

octern
  • 4,825
  • 21
  • 38
  • The patterns I have above work in BBEdit. I'm aware of the resource you linked (it's linked in the question I mentioned), that's what I was referring to as not working in BBEdit, it's also more complex than I need and I'm not sure how to adapt it (it assumes a different context, e.g. 10 or 13 ISBNs, and a prefix of a particular form, different from the one I have). – orome Mar 27 '12 at 21:18
  • Then I totally failed at reading comprehension. Sorry about that :P – octern Mar 27 '12 at 21:21