# How to solve this regular expression problem

**URL:** <https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132>\
**Category:** Ruby\
**Created:** [April 1, 2010, 12:07am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132 "2010-04-01T00:07:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 12:07am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/1 "2010-04-01T00:07:01Z")

</div>

I have strings of this form:

s = ‘C#039;era una volta il east’

I want to replace ‘#039;’ with the octal representation of 39 ie 47 so  
the string becomes

s = ‘C\47era una volta il east’

I have tried this:

1 def conv\_char(num)  
2 puts num.to\_i.to\_s(8)  
3 num.to\_i.to\_s(8)  
4 end  
5  
6 if **FILE** == $0  
7 s = ‘C#039;era’  
8 s.gsub!(/#(\d+);/, conv\_char("\1"))  
9 puts s  
10 end

The outptu i get is  
0  
‘C#0era una volta il east’

I even tried  
8 s.gsub!(/#(\d+);/, conv\_char(’\1’))  
8 s.gsub!(/#(\d+);/, conv\_char($1))

nothing seems to work.

how to solve this gracefully? I guess I can do that using double  
replacement like collecting the matched patterns (#039;) using scan  
method and then converting and doing another gsub using a for loop. But  
that seems to much for the poor regular expression engine.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 12:11am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/2 "2010-04-01T00:11:31Z")

</div>

Amishera Amishera wrote:

> I have strings of this form:
> 
> s = ‘C#039;era una volta il east’
> 
> I want to replace ‘#039;’ with the octal representation of 39 ie 47 so  
> the string becomes
> 
> s = ‘C\47era una volta il east’
> 
> I have tried this:
> 
> 1 def conv\_char(num)  
> 2 puts num.to\_i.to\_s(8)  
> 3 num.to\_i.to\_s(8)  
> 4 end  
> 5  
> 6 if **FILE** == $0  
> 7 s = ‘C#039;era’  
> 8 s.gsub!(/#(\d+);/, conv\_char("\1"))  
> 9 puts s  
> 10 end
> 
> The outptu i get is  
> 0  
> ‘C#0era una volta il east’
> 
> I even tried  
> 8 s.gsub!(/#(\d+);/, conv\_char(’\1’))  
> 8 s.gsub!(/#(\d+);/, conv\_char($1))
> 
> nothing seems to work.
> 
> how to solve this gracefully? I guess I can do that using double  
> replacement like collecting the matched patterns (#039;) using scan  
> method and then converting and doing another gsub using a for loop. But  
> that seems to much for the poor regular expression engine.

I can do this also:

s.gsub!(/#(\d+);/) { puts $1; conv\_char($1) }

But I was wondering how to use both the \1 syntax and the function call  
together in the gsub call in the first approach.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 12:28am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/3 "2010-04-01T00:28:39Z")

</div>

Amishera Amishera wrote:

> I can do this also:
> 
> s.gsub!(/#(\d+);/) { puts $1; conv\_char($1) }
> 
> But I was wondering how to use both the \1 syntax and the function call  
> together in the gsub call in the first approach.

Okay it did solve half of the problem. Other half is how to display a  
‘’ infront.

s.gsub!(/#(\d+);/) { ‘’+$1.to\_i.to\_s(8)}

is giving some compile error.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 1:13am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/4 "2010-04-01T01:13:48Z")

</div>

> s.gsub!(/#(\d+);/) { ‘’+$1.to\_i.to\_s(8)}

> is giving some compile error.

You have to escape the slash () like this

> s.gsub!(/#(\d+);/) { ‘\’+$1.to\_i.to\_s(8)}

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 1:48am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/5 "2010-04-01T01:48:50Z")

</div>

On Wed, Mar 31, 2010 at 6:13 PM, Max S. \<  
removed\_email\_address@domain.invalid\> wrote:

> 

try either of these:

s.gsub!(/#(\d+);/, “\134\134#{conv\_char($)}”)

s.gsub!(/#(\d+);/, “\134\134#{$1.to\_i.to\_s(8)}”)

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 2:29am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/6 "2010-04-01T02:29:34Z")

</div>

OK I was wrong. This does work.

s.gsub!(/#(\d+);/) { ‘\’+$1.to\_i.to\_s(8)}

What I offered does not.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 1, 2010, 1:52am UTC](https://www.ruby-forum.com/t/how-to-solve-this-regular-expression-problem/186132/7 "2010-04-01T01:52:26Z")

</div>

> s.gsub!(/#(\d+);/, “\134\134#{conv\_char($)}”)  
> I meant:  
> s.gsub!(/#(\d+);/, “\134\134#{conv\_char($1)}”)

I’m having trouble doing copy & paste from irb under windows.

David
