Skip to main content
1 of 5
Cleb
  • 307
  • 1
  • 10

What is the correct regular expression for InChI?

When I look for a regular expression for InChI, I succeed on EBI's MIRIAM Registery website:

^InChI\=1S?\/[A-Za-z0-9]+(\+[0-9]+)?(\/[cnpqbtmsih][A-Za-z0-9\-\+\(\)\,\/]+)*$

And indeed it works for some examples:

import re

pattern = '^InChI\=1S?\/[A-Za-z0-9]+(\+[0-9]+)?(\/[cnpqbtmsih][A-Za-z0-9\-\+\(\)\,\/]+)*$'

s = 'InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3'

re.findall(pattern, s)  

will print

[('', '/c1-2-3/h3H,2H2,1H3')]

However, if I take other examples, as e.g. this one

s2 = 'InChI=1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/h2-11H,1H2/t2-,3-,4+,5+,6?/m1/s1'

it fails

re.findall(pattern, s2)

will return

[]

What is the correct and general regular expression for InChI?

Cleb
  • 307
  • 1
  • 10