So when a user enters some text into a textbox I want to find matches with this pattern '[A-Z][a-z]?\d+'
So if a user entered "Hg2H1' it would split it to 'Hg2' and 'H1' and it does. However, if a user were to enter 'Zn4Pb15H2O' the program would split it to 'Zn4' 'Pb15' and 'H2O' which isn't desirable. I would want it to split to 'Zn4' 'Pb1' and '5H2O'. Any help is appreciated, thanks!
Re: Using Regex to find elements?
Posted: Wed Oct 15, 2014 4:41 am
by CodenStuff
I'm not great with Regex but you can find some tools online to help.
The problem is that you can't know whether the user meant Pb15 or Pb1 + 5H2O... as these are two input possibilities.
Edit: Also, I may be wrong, but don't coefficients exist only when typing out a chemical equation? In the case of a chemical formula, I don't think you'll encounter coefficients, just subscripts.
Re: Using Regex to find elements?
Posted: Wed Oct 15, 2014 11:49 pm
by SumCode
comathi wrote:The problem is that you can't know whether the user meant Pb15 or Pb1 + 5H2O... as these are two input possibilities.
Edit: Also, I may be wrong, but don't coefficients exist only when typing out a chemical equation? In the case of a chemical formula, I don't think you'll encounter coefficients, just subscripts.
Yea so I just made it so the user has to enter '*[amt of water]H2O' and I made some custom regex patterns and came out with this code here