Ruby provides Regular Expression replacement via the gsub method of String. For example, if I would like to replace a word I simply need the regex to match the word and the replacement text.
irb(main):006:0> "replacing in regex".gsub /\sin\s/, ' is easy in '
=> "replacing is easy in regex"
The gsub method also lets you use the regex match in the replacement.
irb(main):007:0> "replacing in regex".gsub /\sin\s/, ' is easy0円'
=> "replacing is easy in regex"
If you want part of the regex, but not the whole thing you will need to use a capturing group (parenthesis).
irb(main):008:0> "replacing in regex".gsub /\si(n)\s/, ' is easy i1円 '
=> "replacing is easy in regex"
When using a capturing group that matches multiple times in a single line you can still use
1円 to include the match in the result.
irb(main):015:0> "1%, 10%, 100%".gsub /(\d+)%/, '0.01円'
=> "0.01, 0.010, 0.0100"
Another important detail to note is that I'm using single quotes in my replacement string. Using double quotes neither works with
0円 or
1円 since
"0円" #=> "000円".
irb(main):009:0> "replacing in regex".gsub /\sin\s/, " is easy0円"
=> "replacing is easy000円regex"
irb(main):010:0> "replacing in regex".gsub /\si(n)\s/, " is easy i1円 "
=> "replacing is easy i001円 regex"
7 comments:
Using double quotes neither works with 0円 or 1円 since "0円" #=> "000円".
Reply DeleteActually, all you need do is escape the backslash to keep it from substituting on the initial interpolation pass.
>> suffix = '!'; "fi fie fo fum".gsub(/([aeiou]+)/,"\1円#{suffix}")
=> "fi! fie! fo! fu!m"
Dude you didn't cover the most important think about Ruby gsub that distinguishes it from Perl and other languages. It takes a block argument! The block is passed the string matching the regular expressions and the return value of the block is used as the replacement string. Thus you can do something like the following:
Reply Deleteirb(main):001:0> "blah23blah45".gsub(/\d+/){|num| ((num.to_i)*3).to_s}
=> "blah69blah135"
I was trying to focus on replacement via regex. But, I agree that being able to pass a block to gsub is very cool.
Reply DeleteI am new to Ruby and did not understand the 'block argument' concept.
Reply Delete<<{|num| ((num.to_i)*3).to_s}>>
Can someone explain pl. what its doing? As I remember, even Perl supports expression in a replacement part of a regex, isn't it?
escaping is fine, but another go is:
Reply Delete"you may have to use double-quotas, and keep access on: #{'0円'}"
i wonder if you can do this:
Reply Deletestring.gsub(/#{variable}/)
i.e. substitute a variable into the regexp part, just the way you can do with a string literal in double quotes
this is a better script. the suggested method above would lead to errors when parse_query tries to generate the hashes
Reply Deleterack_input = env["rack.input"].read.gsub(/\&[a-zA-Z]+=/,'nnd0円').gsub("nnd&","^^").gsub("&","^*^").gsub("^^","&")
params = Rack::Utils.parse_query(rack_input, "&")
params.each_pair do |k,y|
params[k] = y.gsub("^*^","&")
end
Note: Only a member of this blog may post a comment.
[フレーム]