--------------060901040603040500040108 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Joe Peck wrote: > It doesn't sound like Chinese :) > > If wouldn't have to be an infinite amount of states. Let's say these > are the states: > > State 1 - no [joe] yet. If finds [joe], goes to state 2. If finds > [/joe], fails. > State 2 - [joe] found but not matching [/joe]. If it finds [joe] again > in this state, then fails. If it finds [/joe], increments count by 1 > and moves to state 1. > > If count goes above 3, fails. > > But maybe I'll use something besides a regexp, although I thought there > would be a pretty easy way to do it. > > You're right, the infinte amount of states is when you have nesting, e.g. parentheses in programming languages. I just came up with this solution, but it looks like that's not what you're after. count '[joe] hello [joe] how are [/joe] you'.scan(/\[joe\]|\[\/joe\]/).each do |m| if m /[joe]/ count+ else count- end end if count! puts "Your joes dont match up!" end A. --------------060901040603040500040108--