From: dblack@... Date: 2006-12-03T10:30:06+09:00 Subject: Re: rexexp question Hi -- On Sun, 3 Dec 2006, ishamid wrote: > [total novice again ;-)] > > Hi, > > Ok, I've figured some of this pattern-matching out (yes, I'm really a > total novice at programming). Consider the following: > > ==================================== > @data.gsub!(/<(text:span > text:style-name="T10")>(.*?)<\/text:span>/mois) do > '{' + '\bf ' + $2 + '}' > end > @data.gsub!(/<(text:span > text:style-name="Style2")>(.*?)<\/text:span>/mois) do > '{' + '\it ' + $2 + '}' > end > @data.gsub!(/(.*?)<\/text:span>/) > do > tag, text = $2, $3 > if inline[tag] then > (inline[tag][0]||'') + clean_display(text) + > (inline[tag][1]||'') > else > clean_display(text) > end > end > ==================================== > > How can I condense this? I want something that says, eg. > > If style-name equals "T10" or "Style2" or "Style3" then do \bf or \it > or \sl respectively. Otherwise, do clean_display (defined elsewhere). > The first line should look like this > > ================== > @data.gsub!(/<(text:span > text:style-name=("T10"|"Style2"|"Style3"))>(.*?)<\/text:span>/mois) do > ================== > > but how do I modify the > > ================== > '{' + '\bf ' + $2 + '}' > ================== > > line? You could perhaps have a hash: replacements = { '"T10"' => "bf", '"Style2"' => "it", '"Style3"' => "sl" } and then replace from the hash: "{\\#{replacements[$1]}#{$2}}" or something like that. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org