Forum: Ruby Another RegExp question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-03-18 12:40
Hi again, I'm allways here to ask how to use regular expressions.
what I want now is to replace chars from a string with empty spaces

sample = "thi's is a tes<>%$#!|t"

what I want is use sample.sub(\['<>%$#!|]\,"")
and the outcome shoudl be

"this is a test"

But I only can replace the first occurence of the chars specified in the
reg exp

How can I replace all occurences?

Thnaks,
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 David A. Black (Guest)
on 2007-03-18 13:03
(Received via mailing list)
Hi --

On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?

Use gsub instead of sub.


David
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2007-03-18 13:04
(Received via mailing list)
J. mp wrote:
> what I want is use sample.sub(\['<>%$#!|]\,"")
> ...
> But I only can replace the first occurence of the chars specified in the
> reg exp
> How can I replace all occurences?

Use gsub instead of sub.
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry Ha (harry)
on 2007-03-18 13:04
(Received via mailing list)
On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:

> How can I replace all occurences?
>
> Thnaks,
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

Will this work ?

sample = "thi's is a tes<>%$###!!!!!###!|t"

x = sample.gsub(/[\''<>%$#!\|]/,"")
puts x

Harry


--

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-18 13:06
(Received via mailing list)
On 18.03.2007 12:40, J. mp wrote:
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?

You can find it in the docs: http://ruby-doc.org/

Hint: There are sub, sub!, gsub and gsub!.

Kind regards

  robert
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry Ha (harry)
on 2007-03-18 13:18
(Received via mailing list)
J. mp <joaomiguel.pereira@gmail.com> wrote:
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?
>

http://www.rubycentral.com/book/ref_c_string.html#...

Harry

--

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry Ha (harry)
on 2007-03-19 02:07
(Received via mailing list)
On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
>
> How can I replace all occurences?
>

But, wouldn't it be easier to do something like this?

sample = "thi's is a tes<>%$###!!!!!###!|t"
y = sample.delete("#!%<'>$|")
puts y

Harry

--

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English
8100138c6c0cd6f3bc27b4fe55c550e5?d=identicon&s=25 doug meyer (Guest)
on 2007-03-20 02:07
(Received via mailing list)
If you want, you could tell gsub to replace everything that is not a-z
or A-Z or a white space character

sample = "thi's is a tes<>%$#!|t"
sample.gsub(/[^a-zA-Z\s]/,"")
"this is a test"
This topic is locked and can not be replied to.