This might not be the final version but covers all cases I have encountered so far (if you find an InChI which cannot be matched with this expression, please post it in the comments along with a source, so that one can adapt the regex):
pattern = '^InChI\=1S?\/[A-Za-z0-9\.]+(\+[0-9]+)?(\/[cnpqbtmsih][A-Za-z0-9\-\+\(\)\,\/\?\;\.]+)*$'
The following changes were made compared to the one posted in the question:
- One also accounts for
.
in the chemical formula; check inchi-trust for an example - One takes
?
into account; check inchi-trust for a detailed explanation - One also checks for
;
; check inchi-trust for an example - One also has to take
.
into account later on, not only in the chemical formula. That is explained in "The IUPIC International Chemical identifier":
/m parity inverted to obtain relative stereo ( 1 = inverted, 0 = not inverted, . = inversion does not affect the parity)
I guess this post will be updated in the future but for my examples it works now and all changes can be backed up by trustworthy sources.
EDIT
The regular expression is updated now to the one I proposed. Apparently, they are happy to change their expression, so if you find cases where a valid InChI
cannot be mapped, please report it to them or add it here in the comments. Clearly, this regular expression might also match strings which are actually not chemicals but at least one should be able to identify each valid InChI
as InChI
.