1

If I wanted to match a string with As,Bs,Cs but my string cannot contain more than one 1 B and no more than 4Cs? How would I do this, without making it very long? I have a solution in which I make every combination of the letter B and arrangement of Cs and A*.

The Regex should match strings like:

AABCCCC

BAAACC

BAACACC

But not things like:

AABB

BBBACCC

CACACACC

parasm
  • 269
  • 1
  • 3
  • 7

2 Answers2

2

You could do it using lookahead:

If, at least 1 B and 1 C are mandatory :

^(?=[^B]*B[^B]*$)(?=(?:[^C]*C[^C]*){1,4}$)[ABC]+$

Else:

^(?=[^B]*B?[^B]*$)(?=(?:[^C]*C[^C]*){0,4}$)[ABC]*$

Explanation:

^                   : begining of line
(?=                 : lookahead, make sure we have:
    [^B]*B[^B]*     : 0 or more non B arround one B
    $               : until end of line
)                   : end lookahead
(?=                 : lookahead, make sure we have:
    (?:             : start non capture group
        [^C]*C[^C]* : 0 or more non C arround one C
    ){1,4}          : 1 upto 4 times
    $               : until end of line
)                   : end lookahead
[ABC]+              : 1 or more any of these letters
$                   : end of line
Toto
  • 89,455
  • 62
  • 89
  • 125
1

This isn't possible with a (basic) regex as counting will make the language larger than the regular language can handle. You can prove this with the pumping lemma for the regular languages

You should list all possibles, when possible.

See also this answer

Community
  • 1
  • 1
Julian
  • 33,915
  • 22
  • 119
  • 174