Forum: Ruby on Rails Gsub patern

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.
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2008-12-22 17:10
(Received via mailing list)
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
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-12-22 17:24
(Received via mailing list)
On Mon, Dec 22, 2008 at 10:09 AM, elioncho <elioncho@gmail.com> 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/...

-Michael

--
Michael C. Libby
www.mikelibby.com
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-22 17:39
(Received via mailing list)
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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
9cf6e7f81d4339295b86e5fb7d7df49b?d=identicon&s=25 Vishwanath Nayak (nayak)
on 2008-12-22 17:43
(Received via mailing list)
Hi,

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

Regards,
NAYAK
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2008-12-22 17:46
(Received via mailing list)
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 Libby" <michael.c.li...@gmail.com>
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2008-12-22 23:25
(Received via mailing list)
On Dec 22, 11:42 am, NAYAK <nay...@gmail.com> 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
marnen@marnen.org
http://www.marnen.org
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-12-22 23:42
(Received via mailing list)
On Mon, Dec 22, 2008 at 10:45 AM, elioncho <elioncho@gmail.com> 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:

http://www.rubular.com/

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


--
Michael C. Libby
www.mikelibby.com
This topic is locked and can not be replied to.