On Fri, May 29, 2009 at 6:49 PM, Robert Klemme <shortcutter / googlemail.com> wrote: > 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/ > > Thanks, Robert. Sometimes I want to avoid repetition and sometimes I want to have repetition. My goal here is not just to solve such a problem. My goal is to learn how to write this type of regular expression using \1, \2, etc. I did not understand the syntax. Thanks, Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html