regex - Referencing named groups in look-around (Python 2.x) -


i have pattern matches multiple key/value pairs, , key/value strings can delimited characters, groups of key/value can delimited, not same character.

i figured out how allow dynamic delimiters, , restrict same delimiter being used twice. eg:

\w+(?p<kv_delim>[:;|])\d+(?p<g_delim>(?!(?p=kv_delim))[:;|])\w(?p=kv_delim)\d(?p=g_delim)? 

you can view regex101.com example here. , works great, problem comes when using either of 2 named groups in positive look-behind.

lets string is

foo:1;r:2

the "key/value delimiter" (named group: kv_delim) :, "group delimiter" (named group: grp_delim) ;

what im trying dynamically match : , ;, in look-around statement, foo<kv_delim>, or bar<kv_delim>.

if hard-code delimiters (in look-around), you can see works. if try reference named-group kv_delim within look-around statement, you can see throws errors. error:

subpattern references not allowed within lookbehind assertion

which whats kickin butt

anybody have way make work?

thanks!

summing has been said: point length of pattern unknown when put backreferences lookbehind must fixed-width @ design time. newer pypi regex module has no limitations regarding lookbehind length, so, current workaround use module regex:

>>> import regex >>> s = "foo:1;r:2" >>> rx = r"\w+(?p<kv_delim>[:;|])\d+(?p<g_delim>(?!(?p=kv_delim))[:;|])\w(?p=kv_delim)\d(?p=g_delim)?" >>> print(regex.findall(rx, s)) [(':', ';')] >>> print([m.group() m in regex.finditer(rx, s)]) ['foo:1;r:2'] >>>  

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -