Todd Horrtyz
Todd Horrtyz

Reputation: 73

Ruby - Escape Parenthesis

I can't for the life of me figure this out, even though it should be very simple.

How can I replace all occurrences of "(" and ")" on a string with "\(" and "\)"?

Nothing seems to work:

"foo ( bar ) foo".gsub("(", "\(") # => "foo ( bar ) foo"

"foo ( bar ) foo".gsub("(", "\\(") # => "foo \\( bar ) foo"

Any idea?

Upvotes: 7

Views: 3873

Answers (4)

Pål Brattberg
Pål Brattberg

Reputation: 4698

Here's what I just used to replace both parens in one call:

str.gsub(/(\(|\))/,'\\\\\1')

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838076

You already have the solution with your second attempt, you were just confused because the string is displayed in escaped form in the interactive interpreter. But really there is only one backslash there not two. Try printing it using puts and you will see that there is in fact only one backslash:

> "foo ( bar ) foo".gsub("(", "\\(")
=> "foo \\( bar ) foo"
> puts "foo ( bar ) foo".gsub("(", "\\(")
foo \( bar ) foo

If you need further convincing, try taking the length of the string:

> "foo ( bar ) foo".length
=> 15
> "foo ( bar ) foo".gsub("(", "\\(").length
=> 16

If it had added two backslashes it would print 17 not 16.

Upvotes: 5

Chubas
Chubas

Reputation: 18043

"foo ( bar ) foo".gsub("(", "\\\\(") does work. If you're trying it in console, you're probably seeing the \\( string because console outputs the string with inspect, that escapes the \

Try with: puts "foo ( bar ) foo".gsub("(", "\\(") and you'll see

Upvotes: 1

Chuck
Chuck

Reputation: 237010

In a string created with double quotes, \ escapes the next character. So in order to get a backslash in the string, you need to escape the backslash itself: "\\(". Or you could just use a single-quoted string, which does less preprocessing: '\('.

Upvotes: 1

Related Questions