[Ruby] regex question

Andrew Nutter-Upham andynu at gmail.com
Mon Feb 12 11:05:01 PST 2007


I find visual feedback helpful when learning or working with complex regexes
try:
http://laurent.riesterer.free.fr/regexp/


On 2/12/07, Mike Mondragon <mikemondragon at gmail.com> wrote:
>
> 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/
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>



-- 
Andy


More information about the Ruby mailing list