-1

I am not a Regular Experssion expert, but my request is simple: I need to match any string that has at least 3 characters of B and at most 2 characters of A

so for example:

"ABABBB"  => Accepted
"ABBBBBB" => Accepted
"BBB"     => Accepted
"ABB"     => Rejected
"AABB"    => Rejected
"AA"      => Rejected
Taleb Ibrahim
  • 11
  • 1
  • 1

3 Answers3

0

I don't think there is any simple solution.

There are 15 ways to write a word with 3 $b$'s and at most 2 $a$'s: $bbb$, $abbb$, $babb$, $bbab$, $bbba$, $aabbb$, $ababb$, $abbab$, $abbba$, $baabb$, $babab$, $babba$, $bbaab$, $bbaba$ and $bbbaa$.

Between every pair of letters of those words, you can insert any number of $b$'s, which can be written with a $b^*$ in a regular expression.

So you'd get: $$bbbb^* + b^*abbbb^* + b^*babbb^*+ … + b^*ab^*abbbb^* + …$$

Nathaniel
  • 18,309
  • 2
  • 30
  • 58
0

A regular expression can be developed from the following finite state machine for at most 2 As and at least 3 Bs by using all possible paths separate by |, and using * where their is a loop edge.

Finite State Machine

Resulting regex: AABBBB*|ABABBB*|ABBABB*|ABBBAB*|ABBBB*AB*|BAABBB*|BABABB*|BABBAB*|BABBB*AB*|BBAABB*|BBABAB*|BBABB*AB*|ABBBB*|BABBB*|BBABB*|BBBAB*|BBBB*(AB*)?(AB*)?

clinux
  • 257
  • 1
  • 7
-1

Wouldn't the answer be:

(b*|b*ab*|b*ab*ab*)|((a|b)*b(a|b)*b(a|b)*b(a|b)*)   ?

greybeard
  • 1,172
  • 2
  • 9
  • 24