2009/5/29 Harry Kakueki <list.push / gmail.com>: > I want to write a regular expression to do the following. > > The first character can be any character. > The second character can be anything except the first character. > The third character can be anything except the first two characters. > (This is a problem. There may be other problems.) > The fourth character is the same as the second character. > > st = "abcb" > p st =~ /^(.)([^\1])([^\1\2])(\2)$/ I expected a match > > uv = "abbb" > p uv =~ /^(.)([^\1])([^\1\2])(\2)$/ I did not expect a match > > > They both match. > How can I write the regular expression? > Somehow I have the syntax wrong. I second Brian: to solve this with a regular expression is either extremely costly (large expression) or even impossible. If I understand you properly then you want to avoid repetitions. How about: require 'set' def unique_chars(s) chars = s.scan /./ chars.to_set.size == chars.size end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/