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