How to write a regular expression for the given language?

Solution 1:

It is in fact trivially possible to make a DFA for the language with $13$ states, $12$ corresponding to "seen $i$ a's and $j$ b's" where $0\le i\le2$ and $0\le j\le 3$ and one reject state. Making a regular expression, though, does require casework on the possible permutations of b's among the (considered fixed) a's; there are $10$ ways to distribute three b's in the three spaces (left, middle and right), and they force $10$ different subexpressions without union: $$\mathtt{b+ab+ab+|b++ab+a|b+ab++a|b++aab+|b+aab++|ab++ab+|ab+ab++|b+++aa|ab+++a|aab+++}$$ where $\mathtt{b+}^n$ is shorthand for $\mathtt b^n\mathtt{b*}$.