# Help with regexp and gsub

**URL:** <https://www.ruby-forum.com/t/help-with-regexp-and-gsub/154390>\
**Category:** Ruby\
**Created:** [November 28, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/help-with-regexp-and-gsub/154390 "2008-11-28T16:18:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 28, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/help-with-regexp-and-gsub/154390/1 "2008-11-28T16:18:47Z")

</div>

I need to be able to match all data before the word “Total:” and then I  
will replace it with “” using gsub.

Here is the text:

C  
THIS IS THE TEXT I WANT TO MATCH ON  
and this…  
and this too…  
and there could be this line too but the following line is always there.  
Total:  
But I need to keep  
all text after  
the text “Total:”

I was thinking that I would somehow use a regexp index but I’m not sure  
how to do it.

thanks  
jackster

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 28, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/help-with-regexp-and-gsub/154390/2 "2008-11-28T16:44:34Z")

</div>

On Fri, Nov 28, 2008 at 4:13 PM, jackster the jackle  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Total:
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I would read the lines into an array or split the string by /\r?\n/

then the following (might be slow for long texts) should do the trick

text.split( /\r?\n/).inject(“”){ …  
or  
lines.inject(“”){  
|text, line|  
if ! trigger.empty? || /^Total/ === line then ## (1)  
text \<\< line.chomp \<\< “\n”  
else  
“”  
end  
}

In case you do not want the line containing Total replace the if with  
the following

if text.empty?  
/^Total/ === line ? “x” : “”  
else  
text \<\< line.chomp \<\< “\n”  
end  
}[2…-1]

HTH  
Robert  
(1) You are absolutely right Charles I tried this with unless…else,  
laughably unreadable

–  
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. 😉

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [November 28, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/help-with-regexp-and-gsub/154390/3 "2008-11-28T16:43:59Z")

</div>

Alle Friday 28 November 2008, jackster the jackle ha scritto:

> Total:  
> But I need to keep  
> all text after  
> the text “Total:”
> 
> I was thinking that I would somehow use a regexp index but I’m not sure  
> how to do it.
> 
> thanks  
> jackster

This should work:

str = \<\<EOS  
line1  
line2  
line3  
Total: text  
line4  
EOS  
new\_str = str.sub(/.\*^Total:/m,’’)

Note the m after the closing slash of the regexp. It means that the  
regexp is  
multiline, that is it means that newline characters (\n) should be  
treated as  
any other character (in particular, the dot will also match them).

I hope this helps

Stefano
