From: Daniel Lucraft Date: 2007-06-24T17:45:30+09:00 Subject: Re: regexp questions Mike Steiner wrote: > I'm getting some odd errors. One problem seems to be that \1 in the > replacement string doesn't always work. Are there any known "gotchas" > between Python's regexps and Ruby's? > > And what's the most robust way to convert "underlined" words to HTML > italics > using a regexp? Something like: "This _word_ is in italics." -> "This > word is in italics." Could you give an example of where it isn't working in the first case? As for the second, I don't know about 'most robust' but I might try something like this (with the assumption that words aren't broken over lines): irb> str => "asdf asdf _asdfasd_ asdf _ash_ h" irb> str.gsub(/_([^\s]+)_/, "\\1") => "asdf asdf asdfasd asdf ash h" Or if I wanted to be able to have italicised sentences (_word word_) I might try this: irb> str => "asdf asdf _asdf asd_ asdf _ash \nash_ h" irb> str.gsub(/_(.+?)_/m, "\\1") => "asdf asdf asdf asd asdf ash \nash h" But then I would worry about performance because of the lazy operator and would want to test it on some real data. best, Dan -- Posted via http://www.ruby-forum.com/.