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.
Elias O. (Guest)
on 2008-12-22 18: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
Michael L. (Guest)
on 2008-12-22 18:24
(Received via mailing list)
On Mon, Dec 22, 2008 at 10:09 AM, elioncho 
<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/...

-Michael

--
Michael C. Libby
www.mikelibby.com
Rob B. (Guest)
on 2008-12-22 18: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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Vishwanath N. (Guest)
on 2008-12-22 18:43
(Received via mailing list)
Hi,

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

Regards,
NAYAK
Elias O. (Guest)
on 2008-12-22 18: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 L." <removed_email_address@domain.invalid>
Marnen L. (Guest)
on 2008-12-23 00:25
(Received via mailing list)
On Dec 22, 11:42 am, NAYAK <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
Michael L. (Guest)
on 2008-12-23 00:42
(Received via mailing list)
On Mon, Dec 22, 2008 at 10:45 AM, elioncho 
<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:

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.