-1

I would like to capture a string with the longest possible amount of one character followed by no more than two of another character. Example:

aaaaabbbbbbbb

Would capture:

aaaaabb, bb and b

I have tried:

(a*)(b)(?!\2{2})

but this is only grabbing the two b's like so:

bb
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
Jay Wehrman
  • 183
  • 1
  • 9

1 Answers1

1
a*b{0,2}

This matches any number of a followed by up to 2 b.

When using your input, it matches

aaaaabb
bb
bb
bb

Since {0,2} is greedy, it always matches pairs of b, not just a single one. It matches all the remaining bb because they're preceded by zero a, which is matched by a*.

DEMO

Barmar
  • 669,327
  • 51
  • 454
  • 560