Suppose I want to match a pattern with the exact same number of characters A and B such that there are exactly n A's followed by exactly n B's. For example, the following strings can be matched.
- AB
- AABB
- AAABBB
On the other hand, these strings cannot be matched
- BA
- AAABB
- AABBB
- ABAB
To approach the problem, I am thinking about the repetition counts, so my attempt looks like this
egrep 'A{n}B{n}'
of course, however, the repetition count n inside the curly bracket cannot be defined implicitly.
While I know how to write programs to match it, I am testing this on Mac terminal, hence I am trying to exploit any possible features of egrep to write the one sentence pattern.
So could anyone please help me solve this problem and any help will be appreciated.