[Ruby] regex question

Mike Mondragon mikemondragon at gmail.com
Mon Feb 12 10:37:32 PST 2007


On 2/12/07, shaners becker <veganstraightedge at gmail.com> wrote:
> i have two questions about regexs:
>
> 1. what's a good starting resource for learning regex? someone told
> me about some ajaxy site that teaches the beginning in and outs of
> regex. anyone know of that?
>
> 2. just in textmate, i'm trying to do a find and replace that needs a
> regex. its an xml file and i want to remove all of one type of tag
> and its contents. say the tag is "book":
>
> <book>moby dick</book>
>
> how do i say find all: <book>...anything here...</book>?
>
> thanks
> shaners
>
>
> -----------
> join the resistance. fall in love.
> http://theresistancearmy.com/

I just practice a regexp in irb to make sure it works correctly.

Sounds like you need a global substitution in place.  Try this out in
irb .* is greedy and assumes that there is only one <book>foo</book>
per line.  Also gsub! modifies the receiver, gsub returns a new
string.

b="<book>moby dick</book>\nhello\n<book>another book</book>"
b.gsub(/<book>.*<\/book>/, "")
b.gsub!(/<book>.*<\/book>/, "")

-- 
Mike Mondragon
http://mondragon.cc/


More information about the Ruby mailing list