[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regex not gsubbing for me.

Peter Bailey

8/20/2007 1:17:00 PM

Hi,
Can someone please explain to me why this simple regex substitution
won't work?

Source data file:
<purpose>Reauthorization of collaborative weather modification
research program within Department of Commerce/NOAA Weather modification
research appropriations</purpose>

My script line:
xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
face="b">Purpose: </emph><entry><text>\1')

The data stays the same. The line isn't converted.

Thanks a lot,
Peter
--
Posted via http://www.ruby-....

7 Answers

Alex Young

8/20/2007 1:20:00 PM

0

Peter Bailey wrote:
> Hi,
> Can someone please explain to me why this simple regex substitution
> won't work?
>
> Source data file:
> <purpose>Reauthorization of collaborative weather modification
> research program within Department of Commerce/NOAA Weather modification
> research appropriations</purpose>
>
> My script line:
> xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
> face="b">Purpose: </emph><entry><text>\1')
>
> The data stays the same. The line isn't converted.
It works for me...

irb(main):001:0> a = "<purpose>Reauthorization of collaborative weather
modification
irb(main):002:0" research program within Department of Commerce/NOAA
Weather modification
irb(main):003:0" research appropriations</purpose>
irb(main):004:0" "
=> "<purpose>Reauthorization of collaborative weather
modification\nresearch program within Department of Commerce/NOAA
Weather modification\nresearch appropriations</purpose>\n"
irb(main):005:0> a.gsub!(/<purpose>(.*)<\/purpose>/mi,
'<row><entry><text><emph
irb(main):006:1' face="b">Purpose: </emph><entry><text>\1')
=> "<row><entry><text><emph\nface=\"b\">Purpose:
</emph><entry><text>Reauthorization of collaborative weather
modification\nresearch program within Department of Commerce/NOAA
Weather modification\nresearch appropriations\n"
irb(main):007:0> a
=> "<row><entry><text><emph\nface=\"b\">Purpose:
</emph><entry><text>Reauthorization of collaborative weather
modification\nresearch program within Department of Commerce/NOAA
Weather modification\nresearch appropriations\n"

--
Alex

Stefan Rusterholz

8/20/2007 1:38:00 PM

0

Peter Bailey wrote:
> Hi,
> Can someone please explain to me why this simple regex substitution
> won't work?
>
> Source data file:
> <purpose>Reauthorization of collaborative weather modification
> research program within Department of Commerce/NOAA Weather modification
> research appropriations</purpose>
>
> My script line:
> xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
> face="b">Purpose: </emph><entry><text>\1')
>
> The data stays the same. The line isn't converted.
>
> Thanks a lot,
> Peter

Your operation changes the string object in memory. Not the file. You
have to write the string back to the file to have it changed there.

Regards
Stefan

--
Posted via http://www.ruby-....

Peter Bailey

8/20/2007 1:45:00 PM

0

Stefan Rusterholz wrote:
> Peter Bailey wrote:
>> Hi,
>> Can someone please explain to me why this simple regex substitution
>> won't work?
>>
>> Source data file:
>> <purpose>Reauthorization of collaborative weather modification
>> research program within Department of Commerce/NOAA Weather modification
>> research appropriations</purpose>
>>
>> My script line:
>> xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
>> face="b">Purpose: </emph><entry><text>\1')
>>
>> The data stays the same. The line isn't converted.
>>
>> Thanks a lot,
>> Peter
>
> Your operation changes the string object in memory. Not the file. You
> have to write the string back to the file to have it changed there.
>
> Regards
> Stefan


Thanks. Yeh, I'm doing that actually. Obviously, this is just a snipped
of my code. I do this at the end of the script:

File.open("F:/workflows/text/in/lobbying/temp/newfile.sgm", "w") { |f|
f.print xmlfile }

But, this little code line above is just one of many code lines just
like it. It's just converting one thing and the others are converting
other things. And, . . ., they're all working!

--
Posted via http://www.ruby-....

Robert Klemme

8/20/2007 1:55:00 PM

0

2007/8/20, Peter Bailey <pbailey@bna.com>:
> Hi,
> Can someone please explain to me why this simple regex substitution
> won't work?
>
> Source data file:
> <purpose>Reauthorization of collaborative weather modification
> research program within Department of Commerce/NOAA Weather modification
> research appropriations</purpose>
>
> My script line:
> xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
> face="b">Purpose: </emph><entry><text>\1')
>
> The data stays the same. The line isn't converted.

Not sure whether that may be related but I'd use (.*?) instead of
(.*). Otherwise you will get just one replacement starting at the
first <purpose> in your file and ending at the last </purpose>.

Btw, are you doing some kind of XML to HTML translation? Then maybe
XSLT is for you.

Kind regards

robert

Peter Bailey

8/20/2007 2:19:00 PM

0

Robert Klemme wrote:
> 2007/8/20, Peter Bailey <pbailey@bna.com>:
>> xmlfile.gsub!(/<purpose>(.*)<\/purpose>/mi, '<row><entry><text><emph
>> face="b">Purpose: </emph><entry><text>\1')
>>
>> The data stays the same. The line isn't converted.
>
> Not sure whether that may be related but I'd use (.*?) instead of
> (.*). Otherwise you will get just one replacement starting at the
> first <purpose> in your file and ending at the last </purpose>.
>
> Btw, are you doing some kind of XML to HTML translation? Then maybe
> XSLT is for you.
>
> Kind regards
>
> robert


Robert-
Yes, this worked, using (.*?) instead of (.*). I've always been putting
that "?" at the end, before the closing "/." And, it never seemed to
work properly. This is great. Thank you very much!

Yes, I am converting XML to SGML and, yes, I have had that suggestion
before, of using XSLT. I'm just learning Ruby now, though, so, I'm
somewhat hesitant to learn yet another scripting language. Although,
I've heard that it isn't hard to learn. I'll take a look when I have
time. (-:

-Peter

--
Posted via http://www.ruby-....

Simon Krahnke

8/20/2007 6:57:00 PM

0

* Peter Bailey <pbailey@bna.com> (16:18) schrieb:

> Yes, I am converting XML to SGML and, yes, I have had that suggestion
> before, of using XSLT. I'm just learning Ruby now, though, so, I'm
> somewhat hesitant to learn yet another scripting language. Although,
> I've heard that it isn't hard to learn. I'll take a look when I have
> time. (-:

Take a look at REXML, it parses XML way better than regular expressions.

mfg, simon .... l

Peter Bailey

8/20/2007 7:26:00 PM

0

Simon Krahnke wrote:
> * Peter Bailey <pbailey@bna.com> (16:18) schrieb:
>
>> Yes, I am converting XML to SGML and, yes, I have had that suggestion
>> before, of using XSLT. I'm just learning Ruby now, though, so, I'm
>> somewhat hesitant to learn yet another scripting language. Although,
>> I've heard that it isn't hard to learn. I'll take a look when I have
>> time. (-:
>
> Take a look at REXML, it parses XML way better than regular expressions.
>
> mfg, simon .... l

Thanks, Simon. Yes, I see that it's actually part of Ruby out of the
box. That's great. I'll look into it.
--
Posted via http://www.ruby-....