Operator for "these expressions, in any order"

Jordi Salvat i Alabart <[email protected]> Wed, 11 Feb 2004 12:06:39 +0100
Newsgroups gmane.comp.jakarta.oro.user
Organization Art Technology Group
Message-ID <[email protected]>
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.

-- 
Thanks for your attention,

Jordi.