1

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?

fadedbee
  • 42,671
  • 44
  • 178
  • 308

1 Answers1

4

Sure, you could do this with positive lookahead assertions:

^(?=.{0,5}a)(?=.{0,5}b)(?=.{0,5}c)(?=.{0,5}d)(?=.{0,5}e)(?=.{0,5}f).*

This will ensure that the letters a through f each appear in the first 6 characters of the string.

Or with a negative lookahead assertion:

^(?!(.).{0,4}\1|.(.).{0,3}\2|..(.).?.?\3|...(.).?\4|....(.)\5)[a-f]{6}

Yeah, I know this looks a little crazy, but basically, it will ensure that the first 6 characters are a through f 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:

^(([a-f])(?<!\2.*\2)){6}

This will ensure that the first 6 characters are a through f and that they do not appear after instance of the same character.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • 1
    +1 Beat me to it. Rather than post my own answer, I'll just link to the demo I was about to post: http://regex101.com/r/tI6dT4/1 – elixenide Jul 22 '14 at 15:48
  • +1 for terrific answer with multiple solutions (more than in [the answer on this near-dupe question](http://stackoverflow.com/a/11143709/1078583)), needs more upvotes. – zx81 Jul 26 '14 at 01:45