From: William James Date: 2006-12-21T07:10:06+09:00 Subject: Re: Regex question: this should be easy but doesn't work as I expect. Sam Kong wrote: > Hi William, > > William James wrote: > > > /(one) two (?:(three) )?four (five)/ > > I simplified the actual problem. > I guess the simplification did not interpret my problem well. > > I was parsing html source into price, image, description, etc. > The image is sometimes missing. > > In the example, let's assume that "two" and "four" are arbiturary text. > So the text might be "...one...three...five" where "..." means some > arbiturary text. > If "three" is missing, it will be "...one.....five...". > > Can you reconsider the problem please? > > Sam This is prolix, but it works: a,b,c = 'one', 'three', 'five' [ "one two three four five", "one two four five" ].each{|s| if s =~ /#{a} (.+ )?#{c}/ if s =~ / #{b} / p [a,b,c] else p [a,nil,c] end end }