I want to match strings which begin with all six characters abcdef
, regardless of the order in which these six characters occur at the beginning of the string.
All six characters must appear once, and once only, in the first six characters of the string, to produce a match.
e.g. "dfabce..." is a match, but "aacdef..." is not.
Can regular expressions do this, or do I need a parser?
Sure, you could do this with positive lookahead assertions:
This will ensure that the letters
a
throughf
each appear in the first 6 characters of the string.Or with a negative lookahead assertion:
Yeah, I know this looks a little crazy, but basically, it will ensure that the first 6 characters are
a
throughf
and that the first, second, third, or fourth, or fifth character are not duplicated within the first 6 characters. You need to have so many different alternations because you don't want the lookahead condition to 'bleed' past the first 6 characters (i.e. you want to match"dfabcee"
).Or alternatively, if your chosen platform supports it, you could use a lookbehind:
This will ensure that the first 6 characters are
a
throughf
and that they do not appear after instance of the same character.