Hi.

In analising some HTML using using regexps, I need to match HTML tags containing certain attributes independently of the order in which these attributes appear -- but ensuring that each of the attributes appears once.

To do this, I would need a reasonably concise and efficient way to build:

from two regexps X and Y, a regexp almost equivalent to

XY|YX

from three regexps X, Y and Z, a regexp almost equivalent to

XYZ|XZY|YXZ|YZX|ZXY|ZYX

... you see the pattern. The "almost" refers to the fact that I would like captured groups in X, Y, Z to be accessible as if only the first case in the alternate (|) construct was there.

I'm currently using

(?:X|Y){2} (?# for the first example) (?:X|Y|Z){3} (?# for the second)

which is very very close to what I need --and works well for my particular job-- but is not formally correct, since it would give a false positive if any of the subexpressions X, Y, Z were matched twice.

Is there any way to 'burn out' those X, Y, Z so that they can only match once? I could use something in the lines of:

(?:(?(1)NOTMATCH|(X))|(?(2)NOTMATCH|(Y))|(?(3)NOTMATCH|(Z))){3}

where NOTMATCH is a regexp that never matches. But it gets unmanageable if X, Y, Z contain capture groups.

Note that this is a problem I often run against when using regexps -- not only in the context of analising HTML. Would it make sense to extend regexp syntax to include a combining operator (like XY or X|Y) meaning 'match these in sequence but in any order?' e.g. X&Y?

All comments welcome.


In reply to Operator for "these expressions, in any order" by jsalvata

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.