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.
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"
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.
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
···
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.
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:
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!
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. (-:
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.
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.