gsub

Ruby, gsub and regex

Quick background: I have a string which contains references to other pages. The pages are linked to using the format: "#12". A hash followed by the ID of the page. Say I have the following string: str = 'This string links to the pages #12 and #125' I already know the IDs of the pages that need linking: page_ids = str.scan(/#(\d*)/)....

More than 9 backreferences in gsub()

How to use gsub with more than 9 backreferences? I would expect the output in the example below to be "e, g, i, j, o". > test <- "abcdefghijklmnop" > gsub("(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)", "\\5, \\7, \\9, \\10, \\15", test, perl = TRUE) [1] "e, g, i, a0, a5" ...

Ruby: how to match a double quote in a regexp

I am trying to remove some double quotes (") characters from a text file using a Ruby one liner, with little success. I have tried the following, and some variations, without success. ruby -pe 'gsub(/\"/,"")' < myfile.txt This gives me the following error: -e:1: Invalid argument - < (Errno::EINVAL) I am running Ruby on a Win machi...

Using gsub to replace a particular character with a newline (Ruby, Rails console)

Hey there Annoying problem. I am trying to replace all semicolon characters in my Model's description field with newline characters (\n). The database is sqlite. The field is of type text. If I do it manually at the rails console (manually typing the description for a single record using \n for line breaks), the rails console automati...

Need some input on how to build a large scale text replacement system

My Rails app deals a lot with data from third-party APIs (specifically UPS, FedEx, DHL, etc). What I'd like to do is whenever that data comes in, replace certain phrases with customized phrases. Example: "On FedEx vehicle for delivery" (which we get from the FedEx API), I'd like to replace with "Out for Delivery." Is it best to replac...

Using gsub to replace double slashes in Ruby

Hi, I am having some issues using gsub to replace double slashes. The problem is this: I built a small script to parse YAML files for a directory location, and then to use that to glob the files in that directory. Say this is the directory it finds: C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes Thi...

Ruby gsub function

Hello I'm trying to create a BBcode [code] tag for my rails forum, and I have a problem with the expression: param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>\1</pre>' ) How do I get what the regex match returns (the text inbetween the [code][/code] tags), and escape all the html and some other characters in it? I've tried thi...

Make one gsub call instead of five.

How can I replace this: lyrics = lyrics.gsub(/\n/,'').gsub(/^\{\"similar\": \[/, '').gsub(/\]\}$/, '').gsub(/^\{/, '').gsub(/\}$/, '') to something shorter and one gsub call? ...

Parallel gsub: how does one remove a different string in each element of a vector

I have a guest list that has a last name in one column and then in another column I have the first names or the full names (first space last) of each person in the family. I am wanting to get the other column to just have the first names. gsub(guest.w$Last.Name,"",guest.w$Party.Name.s.) That would work perfectly if I just had one row...

Ruby gsub doesn't escape single-quotes

I don't understand what is going on here. How should I feed gsub to get the string "Yaho\'o"? >> "Yaho'o".gsub("Y", "\\Y") => "\\Yaho'o" >> "Yaho'o".gsub("'", "\\'") => "Yahooo" ...

Ruby regular expression using gsub

Hi I'm new to Ruby and regular expressions. I'm trying to use a regular expression to remove any zeros from the month or day in a date formatted like "02/02/1980" => "2/2/1980" def m_d_y strftime('%m/%d/%Y').gsub(/0?(\d{1})\/0?(\d{1})\//, $1 + "/" + $2 + "/" ) end What is wrong with this regular expression? Thanks. ...

Ruby Gsub! question

params[:hello] # => "Some params value" hello = params[:hello] hello.gsub!("whatever","") params[:hello] # => "" I don't understand, can someone please explain why the params[:hello] gets modified by the gsub! ? I expected the hello string to be modified, but not the params hash. ...

Replace ' with \' in Ruby?

I'm trying to figure out how to replace a quote like ' with something like \'. How would I do this? I have tried "'".gsub("'","\\'") but it just gives an empty string. What am I doing wrong here? ...

How to escape slashes in single-quote-like string?

ruby 1.8.6 (2007-09-24 patchlevel 111) str = '\&123' puts "abc".gsub("b", str) => ab123c puts "abc".gsub("b", "#{str}") => ab123c puts "abc".gsub("b", str.to_s) => ab123c puts "abc".gsub("b", '\&123') => ab123c puts "abc".gsub("b", "\&123") => a&123c <--- This I want to achieve using temporary variable If I change str = '\&123' to str...

How to gsub slash "/" with back slash and slash "\/" in ruby

I try to modify "/foo/bar/dir" -> "\/foo\/bar\/dir" before execute it in command line. I test by using gsub in irb the result is x = "/foo/bar/dir" x.gsub("/","\\\/") => "\\\/foo\\\/bar\\\/dir" x.gsub("/","\/") => "/foo/bar/dir" Is it possible to replace "/" with "\/" by gsub ? Source of problems is I try to execute "string i...

ruby eval('\1') of gsub possible?

I try to replace a sub-str by the content of a valiable where its name matches the sub-str by: >> str = "Hello **name**" => "Hello **name**" >> name = "John" => "John" str.gsub(/\*\*(.*)\*\*/, eval('\1')) # => error! the last line in the code above is a syntax error. and: >> str.gsub(/\*\*(.*)\*\*/, '\1') => "Hello name" >> str.gsub...

format string (postcode) in ruby

I need to re-format a list of UK postcodes and have started with the following to strip whitespace and capitalize: postcode.upcase.gsub(/\s/,'') I now need to change the postcode so the new postcode will be in a format that will match the following regexp: ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-...

Why I can't substitute with '\\+' inside a string with gsub?

Try the following code: s = '#value#' puts s.gsub('#value#', Regexp.escape('*')) # => '\*' puts s.gsub('#value#', Regexp.escape('+')) # => '' Wtf? It looks like the char '\+' (returned by Regexp.escape) is completely ignored by gsub. How to fix this? ...

smarter character replacement using ruby gsub and regexp

Hi guys! I'm trying to create permalink like behavior for some article titles and i don't want to add a new db field for permalink. So i decided to write a helper that will convert my article title from: "O "focoasă" a pornit cruciada, împotriva bărbaţilor zgârciţi" to "o-focoasa-a-pornit-cruciada-impotriva-barbatilor-zgarciti". Wh...

How to get last 12 digits from a string in MySQL?

Hello. How would I get last 12 digits of a string using mysql? Let's say I have a varchar field with a tracking number, that may be anywhere from 5 to 20 varchars long. But I only need to select last 12 digits or less if there are less. so in a field = 12345678123456789012 I would only need to get what's in brackets field = 1234567...