Reputation: 21512
I have 7 items...
a b c d e f and g
The seven items can be in any order. How to I check with regex that they are there (or not) but no other items are...
^(a)?(b)?(c)?(d)?(e)?(f)?(g)?$
Thad would check for the seven items with any combination of items missing, but only in that order. How do I have the regex check for any possible order of the 7 items?
Both of these would pass:
abcdefg
aceg
I need these to pass as well
bc
fabcd
bgef
I'm using single letters to simplify things. For example (\stest)?
would be an example of one of the items (\skey="([^"<>]+)?")?
is another... I would like to prevent duplicates as well.
These should not pass
abca
aa
gfdef
Upvotes: 0
Views: 77
Reputation: 6513
If you are using php use preg_split seven times with a, b, c, d, e, f, g as the spliter expresion.
Concatenate the result and perform the next.
If any split gives you more than 2 elements you have duplicates
if your resulting final string is different from '' you have invalid parts.
Here you have the code
// checks that any part present is valid but not repeated and not extra stuff
function checkAny($que) {
// atention to the escaping \
$toCheck = array('a','b','c','\\skey="([^"<>]+)?"','d','/slashtest','f','g');
foreach($toCheck as $one){
// $t = preg_split('/'.preg_quote($one,'/').'/', $que); // preg_cuote is not valid for your propouse
$t = preg_split('~'.$one.'~', $que); // so, select a proper sign insted of ~
if(count($t)>2) return 'fail: repeated component/part';
$que = implode('', $t); // can use trim() here if it is usefull for you
if($que=='') return 'pass!!!'; //do not waste time doing any more tests
}
return 'fail: unknown component/part';
}
//test
echo checkAny('abcc'); // fail
echo checkAny('ab/slashtestc'); // fail because the repated a, be careful in test order to avoid this problem
echo checkAny('abcg'); // pass
echo checkAny('ab key="xx"c'); // pass
if php is not the case, preg_replace can be easy substituded on any language supporting regex
Upvotes: 0
Reputation: 301147
Something like this would work:
^(?!(.*(a|b|c|d|e|f|g).*(\2)))((a|b|c|d|e|f|g)+)$
Upvotes: 1