# Gsub patern

**URL:** <https://www.ruby-forum.com/t/gsub-patern/156195>\
**Category:** Rails\
**Created:** [December 22, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195 "2008-12-22T17:10:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/1 "2008-12-22T17:10:26Z")

</div>

Hello,

I am trying to apply a gsub! to a string. I want to know how to make  
the pattern evaluate that everything that IS NOT numbers, letters or  
commas should be replaced with “”(nil). Any ideas in how to do this  
pattern?

Thanks a lot,

Elías

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/2 "2008-12-22T17:24:30Z")

</div>

On Mon, Dec 22, 2008 at 10:09 AM, elioncho [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I am trying to apply a gsub! to a string. I want to know how to make  
> the pattern evaluate that everything that IS NOT numbers, letters or  
> commas should be replaced with “”(nil). Any ideas in how to do this  
> pattern?

irb(main):009:0\> “123\*\*\*,ABC,$$%abc,----+X±—”.gsub(/[^\w,]/, ‘’)  
=\> “123,ABC,abc,X”

Build a character class with []  
Then choose \w and , as characters in the class that you want  
(although note that \w includes all alphanumerics and \_)  
Then negate them both with ^ at the start of the character class

Much more information about Regexp in Ruby can be found here:

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_stdtypes.html#S4](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S4)

-Michael

–  
Michael C. Libby  
[www.mikelibby.com](http://www.mikelibby.com)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/3 "2008-12-22T17:39:11Z")

</div>

On Dec 22, 2008, at 11:09 AM, elioncho wrote:

> Hello,
> 
> I am trying to apply a gsub! to a string. I want to know how to make  
> the pattern evaluate that everything that IS NOT numbers, letters or  
> commas should be replaced with “”(nil). Any ideas in how to do this  
> pattern?

/[^[:alnum:],]/

[^] - is a negated character class  
[:alnum:] - is the POSIX character class for alphanumerics  
, - is a comma 😉

> Thanks a lot,
> 
> Elías

Since you have an accented character in you name, I’m assuming that  
the [:alnum:] class is better than just a-zA-Z0-9 since it should  
encompass the accented characters, too.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/4 "2008-12-22T17:46:16Z")

</div>

Thanks a lot Michael, it worked perfectly.

Thanks for the link too, it cleared some things up.

Elías

On Dec 22, 11:23 am, “Michael L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/5 "2008-12-22T23:25:27Z")

</div>

On Dec 22, 11:42 am, NAYAK [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> You can use gsub(/[^0-9a-zA-Z,]/, ‘’) to replace only non numbers, alphabets  
> and ,

That’s not reliable if you’re outside an English environment. :alnum:  
is a better solution.

> Regards,  
> NAYAK

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/6 "2008-12-22T23:42:26Z")

</div>

On Mon, Dec 22, 2008 at 10:45 AM, elioncho [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks a lot Michael, it worked perfectly.
> 
> Thanks for the link too, it cleared some things up.
> 
> ElÃ­as

I should mention this neat web site, too:

> **[Rubular](https://rubular.com/)**
>
> Ruby-based regular expression editor/tester

You can try out Ruby regular expressions right in your browser and see  
what they match.

–  
Michael C. Libby  
[www.mikelibby.com](http://www.mikelibby.com)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [December 22, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/gsub-patern/156195/7 "2008-12-22T17:43:19Z")

</div>

Hi,

You can use gsub(/[^0-9a-zA-Z,]/, ‘’) to replace only non numbers,  
alphabets  
and ,

Regards,  
NAYAK
