Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm well-aware this answer might be off-topic (as in the approach), but I think it is quite interesting, and it solves the OP's problem.</p> <p>If you don't mind learning a new language (Prolog), then it might help you generate all possible combinations:</p> <pre><code>name(X) :- member(X, ['h', 's', 'hg', 'ga', 'as']). parse_([], []). parse_(InList, [HeadAtom | OutTail]) :- atom_chars(InAtom, InList), name(HeadAtom), atom_concat(HeadAtom, TailAtom, InAtom), atom_chars(TailAtom, TailList), parse_(TailList, OutTail). parse(In, Out) :- atom_chars(In, List), parse_(List, Out). </code></pre> <p>Sample run:</p> <pre><code>?- parse('hgas', Out). Out = [h, ga, s] ; Out = [hg, as] ; false. </code></pre> <hr> <p>The improved version, which includes processing for number is a tad bit longer:</p> <pre><code>isName(X) :- member(X, ['h', 's', 'hg', 'ga', 'as', 'o', 'c']). % Collect all numbers, since it will not be part of element name. collect([],[],[]). collect([H|T], [], [H|T]) :- \+ char_type(H, digit), !. collect([H|T], [H|OT], L) :- char_type(H, digit), !, collect(T, OT, L). parse_([], []). parse_(InputChars, [Token | RestTokens]) :- atom_chars(InputAtom, InputChars), isName(Token), atom_concat(Token, TailAtom, InputAtom), atom_chars(TailAtom, TailChars), parse_(TailChars, RestTokens). parse_(InputChars, [Token | RestTokens]) :- InputChars = [H|_], char_type(H, digit), collect(InputChars, NumberChars, TailChars), atom_chars(Token, NumberChars), parse_(TailChars, RestTokens). parse(In, Out) :- atom_chars(In, List), parse_(List, Out). </code></pre> <p>Sample run:</p> <pre><code>?- parse('hgassc20h245o', X). X = [h, ga, s, s, c, '20', h, '245', o] ; X = [hg, as, s, c, '20', h, '245', o] ; false. ?- parse('h2so4', X). X = [h, '2', s, o, '4'] ; false. ?- parse('hgas', X). X = [h, ga, s] ; X = [hg, as] ; false. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload