-3

Which expression should I use to identify the number of hydrogen atoms in a chemical formula?

For example:

C40H51N11O19 - 51 hydrogens

C2HO - 1 hydrogen

CO2 - no hydrogens (empty)

Any suggestions?

Thanks!

Cheers!

2 Answers2

0

A Regex Expression that will match H with follwowing digits would be:

/H(\d+)/g
  • The 'H' is a literal charecter match to the H in the given chemical formula
  • () declares a capture group, so you cna then grab the captured group without the H in whatever programming language you are using
  • \d will match any digit along with the + modifier that matches 1 or more

There is no catch all scenarios here, you might be best using something other than a regex.

Conner
  • 177
  • 2
  • 7
  • 18
  • There is no catch all, I agree; but this does not even get all the cases OP gave as sample input. E.g. "C2HO" – Yunnosch Sep 26 '17 at 13:18
  • @Yunnosch I agree completely, which is why I suggested using somethying else, however I have added this as an answer for the OP to start off with, maybe give them some guidance on regex. – Conner Sep 26 '17 at 13:20
  • Then maybe something like `([A-Z][a-z]*)(\d+*)` to take into account both digits and element names. – Conner Sep 26 '17 at 13:33
0

You can start using this regex :

H\d*

H -> match literaly the H caracter d* -> match 0 to N time a digit

see exemple and try yourself other regex at : https://regex101.com/r/vdvH8S/2

But regex wont convert for you the result, regex only do lookup.

You need to process your result saying :

  • H with a number : extract the number
  • only H : 1
  • no match : 0
baddger964
  • 1,199
  • 9
  • 18