# Regex not gsubbing for me

**URL:** <https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515>\
**Category:** Ruby\
**Created:** [August 20, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515 "2007-08-20T15:16:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/1 "2007-08-20T15:16:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/2 "2007-08-20T15:22:11Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/3 "2007-08-20T15:42:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/4 "2007-08-20T15:57:15Z")

</div>

2007/8/20, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/5 "2007-08-20T15:37:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/6 "2007-08-20T16:18:26Z")

</div>

Robert K. wrote:

> 2007/8/20, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/7 "2007-08-20T21:26:09Z")

</div>

Simon K. wrote:

> - Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 20, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/regex-not-gsubbing-for-me/112515/8 "2007-08-20T21:06:09Z")

</div>

- Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (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
