From: Mike Cargal Date: 2010-11-04T20:46:26+09:00 Subject: Re: Regular Expression 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' => ============== On Nov 2, 2010, at 5:55 PM, Kendall Gifford wrote: > On Tue, Nov 2, 2010 at 3:34 PM, Richard Conroy wrote: >> On Tue, Nov 2, 2010 at 9:05 PM, Dv Dasari wrote: >> >>> I am trying to write a reqular expression to match a word with my input >>> string. >>> >>> Input string can be ( inpstr = ABCDD ) >>> >>> Any suggestion on how I can fix this? >>> >> >> You are going to need some more advanced regexes to get a match in the way >> you are hoping for. >> >> I would get out a copy of a good regex cheat sheet, and work through some >> examples in http://rubular.com >> http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/pdf/ >> >> The main reason for rubular is that you list all of the regexes you want to >> match, and which to reject, and keep tweaking until you get what you want. >> >> OTOH this regex might scratch your itch: >> /^A?B?C?D{0,2}$/ >> > > The above works so long as each of ABC or D must come in said order, > if present. This goes against the OP's examples: CA, DAD, and BAC. > > My suspicion is that you're problem isn't solvable by a regular > expression alone, but that you'll need to do some parsing (still > possibly using regular expressions in the process). > > -- > Kendall Gifford > zettabyte@gmail.com > Mike Cargal mike@cargal.net http://blog.mikecargal.com