Andrew Johnson wrote: > Joe Peck wrote: > > The problem is I don't want it to accept things like: > > "[joe] hello [joe] how are [/joe] you" > > where there are two opening tags before a closing tag is reached. > > Similarly, I don't want to accept something like: > > "hey [joe] it's hot today[/joe] where [joe] is the ac" > > where there is one correct pair but then an opening tag without a > > closing one. > > > If I am reading your specs correctly: > > /^(((?!\[joe\]).)*(\[joe\]((?!\[joe\]).)+\[\/joe\])){1,3}((?!\[joe\]).)*$/ [ "good [joe] [/joe] [joe] [/joe]", "bad [/joe] [joe] [/joe]", "bad [joe] [/joe] [/joe]" ].each { |s| p s =~ /^(((?!\[joe\]).)*(\[joe\]((?!\[joe\]).)+\[\/joe\])){1,3}((?!\[joe\]).)*$/ }