I would like a regex that would make this:
VALUES('Hit 'n Run')
into
VALUES('Hit ''n Run')
Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, this is not really possible. If you have
VALUES('Hit 'n Run'), you already have an invalid mixture of delimiting apostrophes and literal apostrophes. String processing is like mixing sugar and salt: once you’ve mixed contexts without proper escaping there is no way of pulling them back apart.If you are trying to rescue broken data, you could try something like
(?<!\()'(?!\))to match apostrophes that don’t have a bracket next to them. It’s a weak and easily fooled tactic but for simple data it might work.If you are putting together dynamic SQL queries you must escape the
'before you put it into the query string, either using a simple string replace'with''if you’re sure that’s the only escape your DBMS requires, or — much better — using a dedicated SQL-string-literal-escaping function appropriate to your DBMS. Quite what that function would be depends on what platform (language, DBMS) you’re talking about.