n00b string replace



Hi I'm triying to substitute any ocurrences of ' in a string by \'

Here's a bit of testing I made in irb which wtf's me:

irb(main):002:0> st = "un ' monton ' de ' apostrofes '"
=> "un ' monton ' de ' apostrofes '"
irb(main):003:0> sst = st.gsub(/'/,"\'")
=> "un ' monton ' de ' apostrofes '"

ok substitute ' by ' :P I don't know why interpeter interpret \' as
' when in a double-quoted string contex but I'm a newbie so sure I'm
wrong

irb(main):004:0> sst = st.gsub(/'/,"\\'")
=> "un monton ' de ' apostrofes ' monton de ' apostrofes ' de
apostrofes ' apostrofes "
irb(main):005:0> sst = st.gsub(/'/,"\\\'")
=> "un monton ' de ' apostrofes ' monton de ' apostrofes ' de
apostrofes ' apostrofes "

This seems to be substitute ' by "rest of string from ocurrence"
regardless of scape chars

Totally dazzed and desperate I tried with another backslash.... who
cares ... and get this

irb(main):006:0> sst = st.gsub(/'/,"\\\\'")
=> "un \\' monton \\' de \\' apostrofes \\'"

How do I get "un \' monton \' de \' apostrofes \'" from "un ' monton '
de ' apostrofes '"? using regex?

Thanks
.



Relevant Pages

  • Re: Need help splitting up a fields data
    ... The easy way to parse out that kind of address is to extract what you know, and then remove it from the string, until the unknown part is left. ... So extract the zip code with RightWords, then Substitute out the string in the Zip field in the original field. ... Put that in the state field, then Substitute it out in the original field. ...
    (comp.databases.filemaker)
  • Re: isolate a number in cell with text and numbers
    ... That formula doesn't specifically extract numbers per se, ... have a string of numbers as *the last word*. ... The result of the inner TRIM function is then passed to the SUBSTITUTE ... string with other characters. ...
    (microsoft.public.excel.misc)
  • Re: User command to generate a formatted random number?
    ... substitute, and it will substitute all occurrences of 'a' with 'b'. ... empty strings for 'b'. ... The first pattern is '000$' where the zeros mean literal zeros and $ ... sed seems to replace the first instance of "se" here and not all of ...
    (comp.os.linux.misc)
  • regsub (and regular expressions in general) trouble.
    ... The filename and the surrounding text that will need to be replaced is ... that I can't get it to substitute all of that text, ... set intspiel [string length $textspiel] ... set textfilename [string range $textspiel 11 ...
    (comp.lang.tcl)
  • Re: substitute a specific string
    ... >> I would like to substitute a specific string. ... frustration at your "forcing" me to either go to a newsgroup I've never read ...
    (comp.unix.shell)