From: Kendall Gifford Date: 2010-11-05T02:34:57+09:00 Subject: Re: Regular Expression On Thu, Nov 4, 2010 at 11:27 AM, Kendall Gifford wrote: > 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... > Okay, now this is even closer: regex = /^(A(?=[^A]+$)|B(?=[^B]+$)|C(?=[^C]+$)|D{1,2}(?!D))+$/ However, it still has problems with "DADD", "DADBD" and such... -- Kendall Gifford zettabyte@gmail.com