# Regexp questions

**URL:** <https://www.ruby-forum.com/t/regexp-questions/107609>\
**Category:** Ruby\
**Created:** [June 24, 2007, 12:23am UTC](https://www.ruby-forum.com/t/regexp-questions/107609 "2007-06-24T00:23:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [June 24, 2007, 12:23am UTC](https://www.ruby-forum.com/t/regexp-questions/107609/1 "2007-06-24T00:23:59Z")

</div>

I’m converting a big Python program to Ruby which uses lots of regexps,  
and  
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.”

Thanks!

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [June 24, 2007, 10:45am UTC](https://www.ruby-forum.com/t/regexp-questions/107609/2 "2007-06-24T10:45:10Z")

</div>

Mike S. 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

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [June 24, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/regexp-questions/107609/3 "2007-06-24T18:36:26Z")

</div>

On Sun, Jun 24, 2007 at 05:45:30PM +0900, Daniel L. wrote:

> =\> “asdf asdf _asdf asd_ asdf _ash \nash_ h”  
> irb\> str.gsub(/_(.+?)_/m, “_\1_”)  
> =\> “asdf asdf _asdf asd_ asdf _ash \nash_ h”

I like to be very strict with things like quotes (and underscores in  
this case), so I would probably use:

irb\> str  
=\> “asdf asdf _asdf asd_ asdf _ash \nash_ h”  
irb\> str.gsub(/_([^_]+)\_/, “_\1_”)  
=\> “asdf asdf _asdf asd_ asdf _ash \nash_ h”

That seems to work like I would expect it to—I’m just coming over from  
Perl…

Wyatt

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [June 25, 2007, 4:03am UTC](https://www.ruby-forum.com/t/regexp-questions/107609/4 "2007-06-25T04:03:28Z")

</div>

Thanks for the ideas about the _italics_!

I found out what my “weird” problem was - I wasn’t double-escaping the  
\1 in  
the replacement string (I was using “_\1_” instead of  
“_\1_”.)  
It’s funny how Python didn’t require this. Hmmm.

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [June 24, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/regexp-questions/107609/5 "2007-06-24T20:21:39Z")

</div>

On Jun 24, 2007, at 11:35 , Wyatt Draggoo wrote:

> On Sun, Jun 24, 2007 at 05:45:30PM +0900, Daniel L. wrote:
> 
> > irb\> str.gsub(/_(.+?)_/m, “_\1_”)
> 
> I like to be very strict with things like quotes (and underscores  
> in this case), so I would probably use:
> 
> irb\> str.gsub(/_([^_]+)\_/, “_\1_”)

From a strictness point of view, what’s the difference between /(.+?)  
_/ and /([^_]+)\_/ in the above? AIUI, they’re equivalent. I  
personally like the former because if you need to change the \_ to  
some other character, you only have to make a single character change.

Michael G.  
grzm seespotcode net
