rexexp question

I

ishamid

[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.*?text:style-name=([\'\"])(.*?)\1>(.*?)<\/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?

Best
Idris
 
D

dblack

Hi --

[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.*?text:style-name=([\'\"])(.*?)\1>(.*?)<\/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 | (e-mail address removed)
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
 
I

ishamid

Hi Paul,

ishamid wrote:/ ...

There are quite a few regular posters here who are pretty good with regular
expressions. I am sure we can offer some useful hints if we can see the
data plus the desired outcomes.

Ok, I was trying to avoid typing too much :) but I will post more
detail tomorrow morning. I am new to ruby, this list, and serious
programming so please bear with me as I get the hang of
appropriate/informative communication on this list.

Thank you, Paul!
Idris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

more search and replace 3
OOo and regexp 0
search and replace 4
Tasks 1
Help with my responsive home page 2
XPathAPI not obeying XPath properly 2
[ANN] Ruby-Traits 0.2 released (sorry long) 2
Help with code 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top