Gsub html sanitizer

I’m new to Ruby and a bit confused about how gsub works. I’ve read the
documentation, searched ruby-forum, and tried numerous google searches,
but can’t seem to figure out how to do something that should be simple.

This is just an example, but say I have a string “apple cat flower”. I’d
like to replace all instances of the pattern “apple” with the string
“fruit,” “cat” with the string “animal,” and “flower” with the string
“plant.” Meaning I have more than 1 pattern to replace, each with a
specific string to replace.

So I want “apple cat flower” to be converted into “fruit animal plant”.

I hope this isn’t too confusing. Help is appreciated :slight_smile:

You could just chain this up to make it neater?

orig=“apple cat flower”
orig.gsub(“apple”,“fruit”).gsub(“cat”,“animal”).gsub(“flower”,“plant”)

=> “fruit animal plant”

There might be a ‘smarter’ way of doing this: I’m only just learning
Ruby myself , but I know from other domains (“gsub” is an old ‘awk’
command for instance) that generally speaking when doing regex
search/replace stuff you would tend to actually do this in “n”
iterations rather than coming up with a complex ‘or’ regex…

Also, I know in regex you can use the ‘&’ character to mean “The
resulting matched character(s)” - but in this case you are literally
looking for three distinct patterns…so I’m not sure this is even a
possibility…

The above is just my opinion about using regexes - I would certainly
break this up into three separate gsubs: and I might be missing a trick
or two here…so wait for other posts I would say…

You could write yourself a little ‘method’ here which took two arrays
perhaps, and then iterate over one with the other ? dunno…

John

Chealsea S. wrote:

I’m new to Ruby and a bit confused about how gsub works. I’ve read the
documentation, searched ruby-forum, and tried numerous google searches,
but can’t seem to figure out how to do something that should be simple.

This is just an example, but say I have a string “apple cat flower”. I’d
like to replace all instances of the pattern “apple” with the string
“fruit,” “cat” with the string “animal,” and “flower” with the string
“plant.” Meaning I have more than 1 pattern to replace, each with a
specific string to replace.

So I want “apple cat flower” to be converted into “fruit animal plant”.

I hope this isn’t too confusing. Help is appreciated :slight_smile:

[TYPO correction: I didn’t mean to quote ‘method’ in my reply…I was in
the middle of editing and pressed send to quick , sorry for any
confusion]

2008/9/1 Chealsea S. [email protected]:

So I want “apple cat flower” to be converted into “fruit animal plant”.

I hope this isn’t too confusing. Help is appreciated :slight_smile:

As long as the number of strings is not too large you can use
Regexp.union:

irb(main):004:0> replacements =
{“apple”=>“fruit”,“cat”=>“animal”,“flower”=>“plant”}
=> {“cat”=>“animal”, “apple”=>“fruit”, “flower”=>“plant”}
irb(main):005:0> Regexp.union replacements.keys
=> /cat|apple|flower/
irb(main):006:0> “apple cat
flower”.gsub(Regexp.union(replacements.keys)) {|m| replacements[m]}
=> “fruit animal plant”

If the number increases you probably rather want something like this

irb(main):007:0> “apple cat flower”.gsub(/\w+/) {|m| replacements[m] ||
m}
=> “fruit animal plant”

Kind regards

robert

Thanks everyone for the replies. They were extremely useful.

On Mon, Sep 1, 2008 at 2:09 PM, Chealsea S. [email protected] wrote:

So I want “apple cat flower” to be converted into “fruit animal plant”.

I hope this isn’t too confusing. Help is appreciated :slight_smile:

If you don’t mind a loop, you could gsub each pattern one at a time.

irb(main):002:0> patterns = {“apple” => “fruit”, “cat” => “animal”,
“flower” => “plant”}
=> {“cat”=>“animal”, “apple”=>“fruit”, “flower”=>“plant”}
irb(main):003:0> s = “apple cat flower”
=> “apple cat flower”
irb(main):004:0> patterns.each {|p,r| s.gsub!(p, r)}
=> {“cat”=>“animal”, “apple”=>“fruit”, “flower”=>“plant”}
irb(main):005:0> s
=> “fruit animal plant”

You could also build a big regexp and let gsub do the loop:

irb(main):008:0> s = “apple cat flower”
=> “apple cat flower”
irb(main):009:0> re = Regexp.new("(#{patterns.keys.join("|")})")
=> /(cat|apple|flower)/
irb(main):010:0> s.gsub(re) {patterns[$1]}
=> “fruit animal plant”

Hope this helps,

Jesus.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs