Welcome to the Monastery | |
PerlMonks |
Recursive capture of a variable number of elements using regexpby seaver (Pilgrim) |
on Apr 09, 2009 at 17:46 UTC ( [id://756667]=perlquestion: print w/replies, xml ) | Need Help?? |
seaver has asked for the wisdom of the Perl Monks concerning the following question:
Dear all, I'm tackling a problem for which I could manually hard-code the result, but I'm very aware that I can achieve the same results with a regexp string, which would be more useful... I have a list of chemical formulas, a sample of which is listed below, I simply want to extract, for each formula, the elements within. This is what I've got for my one_liner: My question is, is this the only way I could get through the variable number of groups? I feel like I could write it into the regular expression itself, that the variable number of groups get directly inserted into an array or a hash, and I can exclude the while loop...is this possible? Another question would be, if there is only one atom of an element, then there wouldn't be any output for the second group, but can I convert that empty output into a zero string, "inline"? Thanks Sam
Back to
Seekers of Perl Wisdom
|
|