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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Dec 22, 2008 at 10:09 AM, elioncho <elioncho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -Michael -- Michael C. Libby www.mikelibby.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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íasSince 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, You can use gsub(/[^0-9a-zA-Z,]/, '''') to replace only non numbers, alphabets and , Regards, NAYAK On Mon, Dec 22, 2008 at 9:53 PM, Michael Libby <michael.c.libby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> > On Mon, Dec 22, 2008 at 10:09 AM, elioncho <elioncho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > -Michael > > -- > Michael C. Libby > www.mikelibby.com > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Dec 22, 2008 at 10:09 AM, elioncho <elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > -Michael > > -- > Michael C. Libbywww.mikelibby.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 22, 11:42 am, NAYAK <nay...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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, > NAYAKBest, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org http://www.marnen.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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íasI 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---