Ruby GSUB question

B

Binh Ly

Hello All,

In Ruby if I have the string:

<a href="#" onclick="new
Ajax.Request('/accounts/todo_lists/1/todo_items/2', {asynchronous:true,
evalScripts:true, parameters:'authenticity_token=' +
encodeURIComponent('0523c2646f11e096ef9ecd2f6e5b41690f99fc39')}); return
false;">Remove</a>

How would I go about replacing the single quotes ', with a backslash and
single quote? \'

Ultimately I'd like to arrive at the string below:

<a href="#" onclick="new
Ajax.Request(\'/accounts/todo_lists/1/todo_items/2\',
{asynchronous:true, evalScripts:true, parameters:\'authenticity_token=\'
+ encodeURIComponent(\'0523c2646f11e096ef9ecd2f6e5b41690f99fc39\')});
return false;">Remove</a>

Every variation of GSUB that I've tried has yet to yield that second
string. Any assistance would be greatly appreciated.

Thank you,

Binh
 
P

Peña, Botp

RnJvbTogQmluaCBMeSBbbWFpbHRvOmJpbmhAcGlnYmFieS5uZXRdIA0KIyA8YSBocmVmPSIjIiBv
bmNsaWNrPSJuZXcNCiMgQWpheC5SZXF1ZXN0KCcvYWNjb3VudHMvdG9kb19saXN0cy8xL3RvZG9f
aXRlbXMvMicsIA0KIyB7YXN5bmNocm9ub3VzOnRydWUsDQojIGV2YWxTY3JpcHRzOnRydWUsIHBh
cmFtZXRlcnM6J2F1dGhlbnRpY2l0eV90b2tlbj0nICsNCiMgZW5jb2RlVVJJQ29tcG9uZW50KCcw
NTIzYzI2NDZmMTFlMDk2ZWY5ZWNkMmY2ZTViNDE2OTBmOTlmYzM5JykNCiMgfSk7IHJldHVybg0K
IyBmYWxzZTsiPlJlbW92ZTwvYT4NCiMgSG93IHdvdWxkIEkgZ28gYWJvdXQgcmVwbGFjaW5nIHRo
ZSBzaW5nbGUgcXVvdGVzICcsIHdpdGggYSANCiMgYmFja3NsYXNoIGFuZA0KIyBzaW5nbGUgcXVv
dGU/IFwnDQoNCnlvdSBjYW4gdHJ5IGhhcmRlciA7KQ0KDQpwdXRzICJjJ21vbiwgaXQncyBjb29s
ISIuZ3N1YigiJyIsIlxcXFwnIikNCmNcJ21vbiwgaXRcJ3MgY29vbCENCiM9PiBuaWwNCg0Kb3Ig
c2ltcGx5IGp1c3QsDQoNCnB1dHMgImMnbW9uLCBpdCdzIGNvb2whIi5nc3ViKCInIil7IlxcJyJ9
DQpjXCdtb24sIGl0XCdzIGNvb2whDQoNCg0K
 
L

Lex Williams

I understand the first \\ , as it will produce \ when it's displayed ,
but why are there still 2 more \\ ? As I see it , \\\\' would produce
\\' , and only when it will be printed will we see \' . Could someone
please explain this ?
 
B

Brian Candler

Lex said:
I understand the first \\ , as it will produce \ when it's displayed ,
but why are there still 2 more \\ ? As I see it , \\\\' would produce
\\' , and only when it will be printed will we see \' . Could someone
please explain this ?

Inside a single- or double-quoted string, \ is an escape character, and
to get a literal backslash you have to escape it with another backslash.
So "\\" is a single backslash; "\\\\" is two backslashes.

irb(main):002:0> "\\".size
=> 1

Inside a regexp replacement string, backslash is also an escape
character, because (for example) \1 means "substitute the first capture
group", i.e. the string inside the first set of parentheses.

irb(main):006:0> "abc123def".gsub(/(\d+)/, '*\\1*')
=> "abc*123*def"

So to actually substitute a backslash, the replacement string has to
contain backslash backslash, and you need to write that as "\\\\" or
'\\\\'

irb(main):007:0> "abc123def".gsub(/\d+/, '\\\\')
=> "abc\\def"
^
this is a single backslash in the replacement

If using the block form of gsub then there is no need for the \1 syntax,
since there are other ways of achieving the same thing, and so the
second level of backslash escaping is disabled.

irb(main):009:0> "abc123def".gsub(/(\d+)/) { "*#{$1}*" }
=> "abc*123*def"
irb(main):010:0> "abc123def".gsub(/\d+/) { '\\' }
=> "abc\\def"

Of course, if all this is for escaping SQL, whatever DB access library
you use likely has existing methods to do this for you. e.g. for
ActiveRecord:

n = "o'reilly"
Person.find:)all, :conditions => ["name=?", n])

HTH,

Brian.
 
P

Peña, Botp

RnJvbTogQnJpYW4gQ2FuZGxlciBbbWFpbHRvOmIuY2FuZGxlckBwb2JveC5jb21dIA0KIy4uLg0K
IyBJbnNpZGUgYSBzaW5nbGUtIG9yIGRvdWJsZS1xdW90ZWQgc3RyaW5nLCBcIGlzIGFuIGVzY2Fw
ZSANCiMgY2hhcmFjdGVyLCBhbmQgDQojIHRvIGdldCBhIGxpdGVyYWwgYmFja3NsYXNoIHlvdSBo
YXZlIHRvIGVzY2FwZSBpdCB3aXRoIGFub3RoZXIgDQojIGJhY2tzbGFzaC4gDQojIFNvICJcXCIg
aXMgYSBzaW5nbGUgYmFja3NsYXNoOyAiXFxcXCIgaXMgdHdvIGJhY2tzbGFzaGVzLg0KIy4uLg0K
IyA8c25pcHBlZCBlbmxpZ2h0ZW5pbmcgbWVzc2FnZT4NCg0KDQpvdG9oLCBwZXJsIGFsc28gaGFz
IHJlZ2V4IHBvd2VyIHRvb2xzIGxpa2UgXFEuLlxFIGVnDQooZXNjYXBpbmcgd2l0aG91dCB1c2lu
ZyB0aGUgYmFja3NsYXNoIGllKQ0KDQptYXliZSB3ZSBjb3VsZCBoYXZlIHNvbWV0aGluZyBzaW1p
bGFyPw0KDQo9KQ0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
B

Brian Candler

otoh, perl also has regex power tools like \Q..\E eg
(escaping without using the backslash ie)

maybe we could have something similar?

=)

Interesting; I wasn't aware those worked inside the replacement string,
as well as the regexp.

However, I'm not sure I'd want to copy Perl's ugliness here. From 'man
perlre':

You cannot include a literal "$" or "@" within a "\Q" sequence.
An
unescaped "$" or "@" interpolates the corresponding variable,
while
escaping will cause the literal string "\$" to be matched.
You’ll need
to write something like "m/\Quser\E\@\Qhost/".
...
Beware that if you put literal backslashes (those not inside
interpoâ€
lated variables) between "\Q" and "\E", double-quotish backslash
interâ€
polation may lead to confusing results. If you need to use
literal
backslashes within "\Q...\E", consult "Gory details of parsing
quoted
constructs" in perlop.

That is, if you write s/'/\Q\'\E/ you'd better know what you're doing.

I think I'll live with /#{Regexp.escape("....")}/ instead :)

B.
 
P

Peña, Botp

RnJvbTogQnJpYW4gQ2FuZGxlciBbbWFpbHRvOmIuY2FuZGxlckBwb2JveC5jb21dIA0KIyAuLmVu
bGlnaHRlbmluZyB0YWxrLi4NCiMgSSB0aGluayBJJ2xsIGxpdmUgd2l0aCAvI3tSZWdleHAuZXNj
YXBlKCIuLi4uIil9LyBpbnN0ZWFkIDotKQ0KDQp5b3UncmUgcmlnaHQuICcgdG90YWxseSBmb3Jn
b3QgYWJvdXQgcmVnZXhwI2VzY2FwZSBhcyBhbm90aGVyIHNvbHV0aW9uIHRvIHRoZSBvcC4gaXQg
aXMgbXVjaCBiZXR0ZXIuDQoNCmtpbmQgcmVnYXJkcyAtYm90cA0KDQoNCg==
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top