This is very convenient for some problems:
>>> re.search('(?P<b>.b.).*(?P<i>.i.)', 'abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'}
But what if I don’t know what order to expect ahead of time?
[update]
For example, I have an input variable containing an unknown character order, and it so happens that "b" appears after "i". I want to still be able to refer to groups for ".b". and I.' without having to order my regular expression according to their order in the input var. So, I would like to do something like this, but I don't know if this is possible:
>>> re.search('(?P<b>.b.)|(?P<i>.i.)', unknown_order_alphabet_str).groupdict() {'i': 'hij', 'b': 'abc'}
[final update]
I searched around and tormented my brain with a bunch, but could not create any good results. Guessing this functionality does not exist, because probably the only way to repeat it is to scan the entire line once for each group (which, of course, I could do in a loop instead), but I thought I would look, I had to say about this one.
Thank you for your help,
Josh
python regex unordered
Josh adams
source share