Regex not gsubbing for me

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

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

My script line:
xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \1’)

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

Thanks a lot,
Peter

Peter B. wrote:

xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \1’)

The data stays the same. The line isn’t converted.
It works for me…

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

Stefan R. wrote:

Peter B. wrote:

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

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

My script line:
xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \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!

2007/8/20, Peter B. [email protected]:

xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \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 in your file and ending at the last .

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

Kind regards

robert

Peter B. wrote:

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

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

My script line:
xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \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

Robert K. wrote:

2007/8/20, Peter B. [email protected]:

xmlfile.gsub!(/(.*)</purpose>/mi, ‘Purpose: \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 in your file and ending at the last .

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

Simon K. wrote:

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.

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