-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
  • 607,720
  • 39
  • 448
  • 563
Jay Wehrman
  • 193
  • 2
  • 10

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
  • 741,623
  • 53
  • 500
  • 612