regex not gsubbing for me.

P

Peter Bailey

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
 
A

Alex Young

Peter said:
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"
 
S

Stefan Rusterholz

Peter said:
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
 
P

Peter Bailey

Stefan said:
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!
 
R

Robert Klemme

2007/8/20 said:
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
 
P

Peter Bailey

Robert said:
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
 
S

Simon Krahnke

* Peter Bailey said:
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
 
P

Peter Bailey

Simon said:
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.
 

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

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,000
Latest member
SusannahSt

Latest Threads

Top