From: Kendall Gifford Date: 2010-11-05T02:27:26+09:00 Subject: Re: Regular Expression On Thu, Nov 4, 2010 at 5:46 AM, Mike Cargal wrote: > regex = /^(A(?!A)|B(?!B)|C(?!C)|D{1,2}(?!D))+$/ > > ["ABCDD","CA","CAD","CADD","CADDD","DAD","BAC","BBAD","AABCCD"].each do |string| >   puts "'#{string}' => #{string.match(regex)}" > end > > ============== > 'ABCDD' => ABCDD > 'CA' => CA > 'CAD' => CAD > 'CADD' => CADD > 'CADDD' => > 'DAD' => DAD > 'BAC' => BAC > 'BBAD' => > 'AABCCD' => > ============== However, this also appears by my test to match "DADD", "ABA", etc: ["ABA", "DADD", "CAC"].each do |string| puts "'#{string}' => #{string.match(regex)}" end 'DADD' => DADD 'ABA' => ABA 'CAC' => CAC It does get you closer though. I rarely remember to make use of look-ahead (and look-behind and other "(?X" style patterns) since when switching languages/regexp engines, I'm never sure what features will be there (and will still work the same). I guess I'm too conservative, sticking with core/basic features. This does make me curious how short of a regex using these features could be written for this one case... -- Kendall Gifford zettabyte@gmail.com