regex - Python Regular Expression with optional but greedy groups -
i'm trying write regular expression match string may or may not contain 2 tags. need expression return me 5 elements of string, depending on whether exist, when make tags optional, wildcard bits seem gobble them up:
inputs be:
text{a}more{b}words   {a}text{b}test   text   text{b}text   text{b}   text{a}text  et cetera. thing guaranteed <a> before <b>, provided exist.
my expression looks follows:
^(.*?)(\{a\})?(.*?)(\{b\})?(.*?)$ unfortunately, ends throwing text last group, regardless of whether or not tags present. there way make them greedy, yet keep them optional? re.findall doesn't seem either unfortunately.
any appreciated! :)
try following regex: ^(.*(?={a})|.*?)({a})?(.*(?={b})|.*)({b})?(.*?)$
import re  inputs = ['{a}text{b}test', 'text', 'text{b}text', 'text{b}', 'text{a}text'] p = re.compile(r"^(.*(?={a})|.*?)({a})?(.*(?={b})|.*)({b})?(.*?)$") input in inputs:     print p.match(input).groups() output:
('', '{a}', 'text', '{b}', 'test') ('', none, 'text', none, '') ('', none, 'text', '{b}', 'text') ('', none, 'text', '{b}', '') ('text', '{a}', 'text', none, '') 
Comments
Post a Comment