I'm trying to split the strings of chemicals into their elements and numbers.
See this example
"Fe3O4"
will be split into {"Fe","3","O","4"}
I've tried using StringSplit
and various _LowerCaseQ
type patters but it isn't working. I've also tried using StringSplit[#,""]
to split everything and then finding the lower case characters and putting it back together but I haven't got it to work. Any solution would be greatly appreciated.
Answer
I propose:
StringCases[
{"Fe3O4", "CO", "MgO", "Uut14AuO6"},
DigitCharacter .. | (_?UpperCaseQ ~~ ___?LowerCaseQ)
]
{{"Fe", "3", "O", "4"}, {"C", "O"}, {"Mg", "O"}, {"Uut", "14", "Au", "O", "6"}}
Or as a RegularExpression
:
StringCases[
{"Fe3O4", "CO", "MgO", "Uut14AuO6"},
RegularExpression["\\d+|[A-Z][a-z]*"]
]
Comments
Post a Comment