# Another RegExp question

**URL:** <https://www.ruby-forum.com/t/another-regexp-question/89189>\
**Category:** Ruby\
**Created:** [March 18, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189 "2007-03-18T12:40:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/1 "2007-03-18T12:40:32Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/2 "2007-03-18T13:03:06Z")

</div>

Hi –

On 3/18/07, J. mp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/3 "2007-03-18T13:04:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/4 "2007-03-18T13:04:06Z")

</div>

On 3/18/07, J. mp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I replace all occurences?
> 
> Thnaks,
> 
> –  
> Posted via [http://www.ruby-forum.com/](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](http://www.kakueki.com/ruby/list.html)  
Japanese Ruby List Subjects in English

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/5 "2007-03-18T13:06:08Z")

</div>

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/](http://ruby-doc.org/)

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

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 19, 2007, 2:07am UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/6 "2007-03-19T02:07:39Z")

</div>

On 3/18/07, J. mp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.kakueki.com/ruby/list.html)  
Japanese Ruby List Subjects in English

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 20, 2007, 2:07am UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/7 "2007-03-20T02:07:33Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [March 18, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/another-regexp-question/89189/8 "2007-03-18T13:18:11Z")

</div>

J. mp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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#String.gsub](http://www.rubycentral.com/book/ref_c_string.html#String.gsub)

Harry

–

[http://www.kakueki.com/ruby/list.html](http://www.kakueki.com/ruby/list.html)  
Japanese Ruby List Subjects in English
