how to solve this regular expression problem

A

Amishera Amishera

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.
 
A

Amishera Amishera

Amishera said:
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.
 
A

Amishera Amishera

Amishera said:
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.
 
D

David Springer

[Note: parts of this message were removed to make it a legal post.]

On Wed, Mar 31, 2010 at 6:13 PM, Max Schmidt <
You have to escape the slash (\) like this
try either of these:

s.gsub!(/#(\d+);/, "\134\134#{conv_char($)}")

s.gsub!(/#(\d+);/, "\134\134#{$1.to_i.to_s(8)}")
 
D

David Springer

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
 
D

David Springer

OK I was wrong. This does work.

s.gsub!(/#(\d+);/) { '\\'+$1.to_i.to_s(8)}

What I offered does not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top