< /p>
pattern = "(%u%l*)(%d*)"
The first set of trapped atomic symbols (ie “H”, “He ”Etc.), the number of the atom in the second group of capture molecules. This value is usually an integer value, but if it is 1, it is usually omitted, for example:
formula = "C2H6O"
When I try to make a global match, if there is no match, the count result is “not nil as I expected.
compound = {}
for atom,count in string.gmatch(formula, pattern) do
compound[atom] = count or 1
end
Obviously I can check count =” But I am curious whether there are optional capture groups in Lua.
if there was an optional capturing group in Lua.
No; pattern items will not list the capture list as an acceptable option, so you can’t have (%d*)? Just like you did in Perl.
I tried to use simple pattern matching to parse chemical formulas in Lua. However, I don’t know how to specify capture groups as available Choose. This is the pattern I proposed:
pattern = "(%u%l*)(%d*)"
One set of trapped atom symbols (ie “H”, “He”, etc.), and the second set of trapped molecules the number of that atom. This value is usually an integer value, but if it is 1, it is usually omitted, for example: < /p>
formula = "C2H6O"
When I try to make a global match, if there is no match, the count result is “not nil as I expected.< /p>
compound = {}
for atom,count in string.gmatch(formula, pattern) do
compound[atom] = count or 1
end
Obviously I can check count=” but I am curious whether there is an optional capture group in Lua.
if there was an optional capturing group in Lua.
No; pattern items will not list the capture list as an acceptable option, so you cannot have it (%d *) ? Just like you do in Perl.