Using Regex to find elements?
Post your questions regarding programming in C# in here.
4 posts
Page 1 of 1
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!
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!
I'm not great with Regex but you can find some tools online to help.
http://regexstorm.net/
http://txt2re.com/
http://regexhero.net/tester/
http://www.ultrapico.com/Expresso.htm
http://www.textpression.com/
http://www.linuxintro.org/regex/
http://regexstorm.net/
http://txt2re.com/
http://regexhero.net/tester/
http://www.ultrapico.com/Expresso.htm
http://www.textpression.com/
http://www.linuxintro.org/regex/
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
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.
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.
comathi wrote:The problem is that you can't know whether the user meant Pb15 or Pb1 + 5H2O... as these are two input possibilities.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
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.
Code: Select all
So if I were to enter an equation like this 'Pb2(Hg1O2)1He1*2H2O' the program would see it as
String useWaters = equationTB.Text.Contains("H2O") ? Regex.Match(equationTB.Text, @"\*\d+(H2O)").Value : "";
String equation = useWaters != "" ? equationTB.Text.Replace(useWaters, "") : equationTB.Text;
MatchCollection allMatches = Regex.Matches(equation, @"\(?[A-Z][a-z]?\d+\)?\d*");
for (int i = 0; i <= allMatches.Count - 1; i++)
{
String symbol, amount = "";
String value = allMatches[i].Value;
watersAmount.Text = useWaters != "" ? Regex.Match(useWaters, @"\d+").Value : "0";
symbol = Regex.Match(value, "[A-Z][a-z]?").Value;
if (value.Contains('('))
{
amount = Convert.ToString(Convert.ToInt32(Regex.Match(value, @"\d+").Value) * Convert.ToInt32(allMatches[i].NextMatch().Value.Split(')')[1]));
}
else if (value.Contains(')'))
{
amount = Convert.ToString(Convert.ToInt32(value.Split(')')[1]) * Convert.ToInt32(value.Split(')')[0].Replace(symbol, "")));
}
else
{
amount = Regex.Match(value, @"\d+").Value;
}
switch (i)
{
case 0:
firstElement.SelectedIndex = allElements.FindIndex(k => k.elementSymbol == symbol);
firstAmount.Text = amount;
break;
case 1:
secondElement.SelectedIndex = allElements.FindIndex(k => k.elementSymbol == symbol);
secondAmount.Text = amount;
break;
case 2:
thirdElement.SelectedIndex = allElements.FindIndex(k => k.elementSymbol == symbol);
thirdAmount.Text = amount;
break;
case 3:
fourthElement.SelectedIndex = allElements.FindIndex(k => k.elementSymbol == symbol);
fourthAmount.Text = amount;
break;
case 4:
fifthElement.SelectedIndex = allElements.FindIndex(k => k.elementSymbol == symbol);
fifthAmount.Text = amount;
break;
default:
return;
}
}
Pb - 2And end-result is
Hg - 1
O - 2
H2O - 2
Also just in case anyone is taking chemistry like me, here's the source
You do not have the required permissions to view the files attached to this post.
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023