Conditional regular expression with one section dependent on the result of another section of the regex -
is possible design regular expression in way part of dependent on section of same regular expression?
consider following example:
(abchehg)[hge]{5,1230}(eejopk)[dm]{5}
i want continue regex, , @ point have section result of section should depend on result of [dm]{5}.
for example, d complemented c, , m complemented n.
(abchehg)[hghe]{5,1230}(eejopk)[dm]{5}[acf]{1,1000}(bbba)[cu]{2,5}[d'm']{5}
by d' mean c, , m' mean n.
so resulting string matches above regex, if has dddmm matching section [dm]{5}, should have cccnn matching [d'm']{5}. therefore, result of [d'm']{5} depends on [dm]{5}, or in other words, matches [dm]{5} dictates match [d'm']{5}.
is possible such thing regex?
please note that, in example have extremely over-simplified problem. regex pattern have more complex , longer , my actual pattern includes 5-6 of such dependent sections.
i cannot think of way can in pure regex. run 2 regex expressions. first regex extract [dm]{5} string, such as
(abchehg)[hghe]{5,1230}(eejopk)[dm]{5}
and take last 5 characters. replace characters, example in c# result = result.substring(result.length - 5, 5).replace('d', 'c').replace('m', 'n')
, , concatenate like
(abchehg)[hghe]{5,1230}(eejopk)[dm]{5}[acf]{1,1000}(bbba)[cu]{2,5}
+ result
Comments
Post a Comment