I have always had hard time understanding regular expressions. With the help of web searches i have always managed to pull through somehow. Guess i have never bothered to really learn then. Sorry.
But i need help with them yet again.
I have a dict like
d = {'account_id':a_value,'group_id':g_value,'other_id':o_value }
And i have bunch of strings like:
s1 = r'^settings/usergroups/(?P<group_id>\d+)/cargroups/$'
s2 = r'^settings/usergroups/(?P<group_id>\d+)/other/(?P<other_id>\d+)/$',
s3 = r'^settings/account/(?P<account_id>\d+)/other/(?P<other_id>\d+)/$',
How can O replace the (?P< group_id >\d+), (?P< account_id >\d+), (?P< other_id >\d+) in the strings with matching values from dict?
Regular expressions can contain nested parentheses.
However, Python regular expressions can not match strings containing arbitrarily
deep nested parentheses in a way that respects the nested structure. (It is possible using Perl’s recursive regular expressions.)
So, if your use case involves strings that DO NOT contain nested paretheses,
then the following suffices, but note carefully the undesired extra parenthesis in the last result below:
yields
If you do need to parse nested parentheses, then you’ll need a different parser than
re. Pyparsing, for example, can handle nested expressions.