# Regex trick

**URL:** <https://www.ruby-forum.com/t/regex-trick/101391>\
**Category:** Ruby\
**Created:** [February 5, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/regex-trick/101391 "2006-02-05T23:11:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 5, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/1 "2006-02-05T23:11:13Z")

</div>

I want to replace all ’ by ’ ? (I need it to do inserts in mysql).

> s = “j’ai”  
> s.gsub!(/’/,’’’)  
> p s ==\> gives j’ai and not j’ai as I want  
> Thanks for help.

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 5, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/2 "2006-02-05T23:17:18Z")

</div>

On Monday, February 06, 2006, at 7:08 AM, Alain FELER wrote:

> I want to replace all ’ by ’ ? (I need it to do inserts in mysql).
> 
> > s = “j’ai”  
> > s.gsub!(/’/,’’’)  
> > p s ==\> gives j’ai and not j’ai as I want  
> > Thanks for help.

s.gsub!("’", “\\’”)

should do the trick.

Regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 5, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/3 "2006-02-05T23:20:21Z")

</div>

On 5-Feb-06, at 5:08 PM, Alain FELER wrote:

> I want to replace all ’ by ' ? (I need it to do inserts in mysql).
> 
> > s = “j’ai”  
> > s.gsub!(/‘/,’'')  
> > p s ==\> gives j’ai and not j'ai as I want  
> > Thanks for help.

s.gsub!(/‘/, "\\’")

or

s.gsub!(/‘/) { "\’" }

will do what you want

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.co.uk/~mike/](http://www.stok.co.uk/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 5, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/4 "2006-02-05T23:26:26Z")

</div>

Alain FELER wrote:

> I want to replace all ’ by ’ ? (I need it to do inserts in mysql).
> 
> > s = “j’ai”  
> > s.gsub!(/’/,’’’)  
> > p s ==\> gives j’ai and not j’ai as I want  
> > Thanks for help.  
> > irb(main):011:0\> s = “j’ai”  
> > =\> “j’ai”  
> > irb(main):012:0\> puts s.sub(/’/) { ‘\’’ }  
> > j’ai  
> > =\> nil  
> > irb(main):013:0\>

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 5, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/5 "2006-02-05T23:26:27Z")

</div>

On 5-Feb-06, at 5:08 PM, Alain FELER wrote:

> I want to replace all ’ by ' ? (I need it to do inserts in mysql).
> 
> > s = “j’ai”  
> > s.gsub!(/‘/,’'')  
> > p s ==\> gives j’ai and not j'ai as I want  
> > Thanks for help.

What interface to MySQL are you using? I know that the Ruby DBI  
module allows you to use place-holders in queries, and the DBI layer  
does the escaping for you e.g.

```
dbh.do("INSERT INTO people (id, name, height) VALUES(?, ?, ?)",
        nil, "Na'il", 76)

```

The resulting statement produced by do and sent to the server looks  
like this:

```
INSERT INTO people (id,name,height) VALUES(NULL,'Na\'il',76)

```

(stolen from [http://www.kitebird.com/articles/ruby-dbi.html](http://www.kitebird.com/articles/ruby-dbi.html)) or if  
you are using the ruby mysql interface then (from http://  
[www.kitebird.com/articles/ruby-mysql.html](http://www.kitebird.com/articles/ruby-mysql.html))

Using escape\_string, the platypus record might be inserted as  
follows:

```
name = dbh.escape_string("platypus")
category = dbh.escape_string("don't know")
dbh.query("INSERT INTO animal (name, category)
        VALUES ('" + name + "','" + category + "')")

```

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.co.uk/~mike/](http://www.stok.co.uk/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 7, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/6 "2006-02-07T23:16:53Z")

</div>

On Feb 7, 2006, at 1:43 PM, Alain FELER wrote:

> Thank you very much for these four answers, but curiously none  
> works on my box ! :

Incorrect. They all work.

p “j\'ai”  
“j\'ai”  
puts “j\'ai”  
j'ai

> Stefan L. : s.gsub!(“'”, “\\'”) gives “j\'ai” not “j'ai”

puts “j’ai”.gsub(“'”, “\\'”)  
j'ai

> Timothy H. : s.sub(/'/) { ‘\'’ } gives “j\'ai”

puts “j’ai”.gsub(/'/) { ‘\'’ }  
j'ai

> you : s.gsub!(/‘/, "\\’") gives “j\'ai”

puts “j’ai”.gsub(/‘/, "\\’")  
j'ai

> and : s.gsub!(/‘/) { "\’" } gives “j’ai”

puts “j’ai”.gsub(/‘/) { "\’" }  
j'ai

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 7, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/7 "2006-02-07T23:26:06Z")

</div>

Well, sorry:

> p s gives “j\'ai”  
> puts s gives j’ai  
> print s gives j’ai  
> so every four methods work if the result is properly printed  
> AF

Alain FELER a écrit :

---

<div class="post-metadata">

**Author:** ![Alain\_FELER](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Alain\_FELER](https://www.ruby-forum.com/u/Alain_FELER)\
**Post date:** [February 7, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/regex-trick/101391/8 "2006-02-07T22:46:23Z")

</div>

Thank you very much for these four answers, but curiously none works on  
my box ! :  
Stefan L. : s.gsub!("’", “\\’”) gives “j\'ai” not “j’ai”  
Timothy H. : s.sub(/’/) { ‘\’’ } gives “j\'ai”  
you : s.gsub!(/’/, “\\’”) gives “j\'ai”  
and : s.gsub!(/’/) { “\’” } gives “j’ai”  
(I am using ruby 1.8.2.14 on Windows 2000 configured in french,  
and I can’t even type the {} characters in irb, but I tried with Scite)  
However the ruby mysql interface escape\_string method works fine.  
Thank you.  
AF

Mike S. a écrit :
