Ruby gsub multiple characters in string
String#gsub
and Hash#fetch
will be the good choice for this.
a = "every good boy does fine"
h = {"every" => "all","boy" => "girl", "fine" =>"well" }
a.gsub(/\w+/) { |m| h.fetch(m,m)}
# => "all good girl does well"
or,
a = "every good boy does fine"
h = {"every" => "all","boy" => "girl", "fine" =>"well" }
Regexp.new("^#{h.keys.join('|')}$") # => /^every|boy|fine$/
a.gsub(Regexp.new("^#{h.keys.join('|')}$"),h)
# => "all good girl does well"
How to replace multiple substrings with same string using `gsub`
to|an|a|the
is pattern, you are using it as String. Here:
str.gsub('to|an|a|the', '') # passing string argument
#=> "How to chop an onion?"
str.gsub(/to|an|a|the/, '') # passing pattern argument
#=> "How chop onion?"
Ruby multiple string replacement
Since Ruby 1.9.2, String#gsub
accepts hash as a second parameter for replacement with matched keys. You can use a regular expression to match the substring that needs to be replaced and pass hash for values to be replaced.
Like this:
'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"
'(0) 123-123.123'.gsub(/[()-,. ]/, '') #=> "0123123123"
In Ruby 1.8.7, you would achieve the same with a block:
dict = { 'e' => 3, 'o' => '*' }
'hello'.gsub /[eo]/ do |match|
dict[match.to_s]
end #=> "h3ll*"
Replacing multiple characters in Ruby string with one character
The easiest I can think of is:
string.tr!("aeiou", "*")
Replace multiple strings in one gsub() or chartr() statement in R?
You can use gsubfn
library(gsubfn)
gsubfn(".", list("'" = "", " " = "_"), x)
# [1] "ab_c"
Similarly, we can also use mgsub
which allows multiple replacement with multiple pattern to search
mgsub::mgsub(x, c("'", " "), c("", "_"))
#[1] "ab_c"
How to have gsub handle multiple patterns and replacements
Well, as you can read from the docs, gsub
does not handle multiple patterns and replacements at once. That's what causing your error, quite explicit otherwise (you can read that as "give me a String, not an Array!!1").
You can write that like this:
def twitterize(text)
patterns = [/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, /(?<=^|(?<=[^a-zA-Z0-9-\.]))@([A-Za-z_]+[A-Za-z0-9_]+)/, /(?<=^|(?<=[^a-zA-Z0-9-\.]))#([A-Za-z_]+[A-Za-z0-9_]+)/]
replacements = ["<a href='\\0' target='_blank'>\\0</a>",
"<a href='http://twitter.com/\\1' target='_blank'>\\0</a>",
"<a href='http://twitter.com/search?q=\\1&src=hash' target='_blank'>\\0</a>"]
patterns.each_with_index do |pattern, i|
text.gsub!(pattern, replacements[i])
end
text
end
This can be refactored into more elegant rubyish code, but I think it'll do the job.
Using gsub to strip multiple characters
You should do it in once:
string[/yyyy(.*)zzzz\z/, 1]
GSUB replace 3 or more repeating characters
When you enclose the whole pattern inside square brackets, you make it match a single char.
Your regexps mean:
[\/\\1{3,}]
- a single char,/
,\
,1
,{
,3
,,
or}
[\/\2+]
-/
,\u0002
char or+
[\/{3,}]
-/
,{
,3
,,
or}
You can use
s.gsub(/\/{3,}/, '//')
See the Ruby demo online.
Regex for multiple string operations in a single pass?
This uses String#tr
to do the substitution in a single pass. This assumes the string consists of printable ASCII characters.
string.tr " \t\nB-DF-HJ-NP-TV-Zb-df-hj-np-tv-z!-@[-`{-~", '&&&*'
# => "*a*e&a**&**e&**a**e*&*i**i*&a&*i***e&*a**"
For tr
, -
is the range operator. So for the letters B
, C
, D
, since these are consecutive, it can be written as B-D
. So B-DF-HJ-NP-TV-Z
is basically all the capital letters minus the vowels. Same with lowercase, followed by all printable punctuation on the ASCII chart. These all get replaced by a *
. The only 3 whitespace characters that match \s
are space, tab, and newline, and these are listed explicitly at the front of the string and are each replaced by &
.
If 2 passes are allowed, then it can be written more concisely as
string.tr(' ','&').tr('^AEIOUaeiou&','*')
Related Topics
Get Sidekiq to Execute a Job Immediately
How Does Ruby's Sort_By {Rand} Work
Ruby (And Rails) Nested Module Syntax
How to Test 'Rand()' with Rspec
Ruby Forgets Local Variables During a While Loop
Why Is Gets Throwing an Error When Arguments Are Passed to My Ruby Script
301 Moved Permanently After S3 Uploading
How to Use Params with Slashes with Sinatra
Can't Install Gems Because "Undefined Method 'Invoke_With_Build_Args' for Nil:Nilclass"
What's the Best/Easiest Gui Library for Ruby
Rails Unable to Convert Unpermitted Parameters to Hash
Setting Path for Whenever in Cron So It Can Find Ruby
Rails Put Validation in a Module Mixin
How to Change Column Type in Heroku
How to Format Irb Command Prompt
How to Deal with Memory Leaks in Rmagick in Ruby
Error: Failed to Build Gem Native Extension (Ruby Extconf.Rb): MAC Osx
The Number of Trailing Zeros in a Factorial of a Given Number - Ruby