Skip to main content
5 of 5
added 29 characters in body
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 (using Python's re module):

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?

EDIT

If I see this correctly, in the pattern from above, question marks are not taken into account which are used to mark both “unknown” and “undefined” chiral centres. If one adjusts the regex accordingly, it at least works for the example above; whether it then holds for all InChI is unclear, of course.

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

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'

re.findall(pattern, s2)

will return

[('', '/c7-1-2-3(8)4(9)5(10)6(11)12-2/h2-11H,1H2/t2-,3-,4+,5+,6?/m1/s1')]
Cleb
  • 307
  • 1
  • 10