Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby string sub without regex back references

I'm trying to do a simple string sub in Ruby.

The second argument to sub() is a long piece of minified JavaScript which has regular expressions contained in it. Back references in the regex in this string seem to be effecting the result of sub, because the replaced string (i.e., the first argument) is appearing in the output string.

Example:

input = "string <!--tooreplace--> is here"
output = input.sub("<!--tooreplace-->", "\&")

I want the output to be:

"string \& is here"

Not:

"string & is here"

or if escaping the regex

"string <!--tooreplace--> is here"

Basically, I want some way of doing a string sub that has no regex consequences at all - just a simple string replace.

like image 402
Andy Hume Avatar asked Nov 15 '11 12:11

Andy Hume


People also ask

What is =~ in Ruby?

=~ is Ruby's basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter.

How do you replace a substring in Ruby?

Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string.

What method should you use when you want to get all sequences matching a regex pattern in a string?

To find all the matching strings, use String's scan method.

What is sub in Ruby?

The sub() method replaces just the first instance of a string with another. Gsub meanwhile replaces all instances. Thus:Gsub is closest to a “replace string” method. Sub() is conceptually a “replace first string” method. Ruby program that compares sub, gsubvalue = "abc abc"


2 Answers

To avoid having to figure out how to escape the replacement string, use Regex.escape. It's handy when replacements are complicated, or dealing with it is an unnecessary pain. A little helper on String is nice too.

input.sub("<!--toreplace-->", Regexp.escape('\&'))
like image 120
Dave Newton Avatar answered Sep 29 '22 20:09

Dave Newton


You can also use block notation to make it simpler (as opposed to Regexp.escape):

=> puts input.sub("<!--tooreplace-->") {'\&'}
string \& is here
like image 44
Matt Avatar answered Sep 29 '22 20:09

Matt