Hello champs, I am having an string and i want all extra characters to be removed from that string except + and . How to write a regular expression for it.. Currently i am trying it like this as word.sub(/[_-\:\\\/]/, "").. But its not working.. I can do it like this as well:- word.sub(/[\W]/, "") but this will replace all characters even also + and . -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Give sample input string and output string.. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Le 07/04/2010 11:44, Hemant Bhargava a écrit :> Hello champs, > > I am having an string and i want all extra characters to be removed from > that string except + and . > How to write a regular expression for it.. Currently i am trying it like > this as word.sub(/[_-\:\\\/]/, "").. But its not working.. > > I can do it like this as well:- word.sub(/[\W]/, "") but this will > replace all characters even also + and . >This seems to be what you need : str = "ABCD EF + (GH_IJ).klm _ ^^z" str.gsub(/[^\w\+\.]|\_/i, '''') => "ABCDEF+GHIJ.klmz" -- Aurélien AMILIN -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Siddick Ebramsha wrote:> Give sample input string and output string..Hey thanks for such a quick reply.. Ok lets suppose, Input:- :Hemant.bhargava+_7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Output should be as only, Hemant.bhargava+7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Means that i want to remove all extra chars except . and +.. Hope u got it now.. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 April 2010 10:44, Hemant Bhargava <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am having an string and i want all extra characters to be removed from > that string except + and .word.gsub(/[^+\.]/, "") -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 April 2010 10:58, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7 April 2010 10:44, Hemant Bhargava <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I am having an string and i want all extra characters to be removed from >> that string except + and . > > word.gsub(/[^+\.]/, "") >oh... if you want to keep alphanumeric characters: word.gsub(/[^a-zA-Z0-9+\.]/, "") -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The question or the example isn''t clear enough. But for the example you gave, this works = a.sub(/[\s\_]/,'''') Thanks & Regards, Dhruva Sagar. On Wed, Apr 7, 2010 at 15:25, Hemant Bhargava <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Siddick Ebramsha wrote: > > Give sample input string and output string.. > > Hey thanks for such a quick reply.. > > Ok lets suppose, > Input:- :Hemant.bhargava+_7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <Hemant.bhargava%2B_7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Output should be as only, Hemant.bhargava+7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org<Hemant.bhargava%2B7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > Means that i want to remove all extra chars except . and +.. Hope u got > it now.. > -- > Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
AMILIN Aurélien wrote:> Le 07/04/2010 11:44, Hemant Bhargava a �crit : >> Hello champs, >> >> I am having an string and i want all extra characters to be removed from >> that string except + and . >> How to write a regular expression for it.. Currently i am trying it like >> this as word.sub(/[_-\:\\\/]/, "").. But its not working.. >> >> I can do it like this as well:- word.sub(/[\W]/, "") but this will >> replace all characters even also + and . >> > This seems to be what you need : > > str = "ABCD EF + (GH_IJ).klm _ ^^z" > str.gsub(/[^\w\+\.]|\_/i, '''') > => "ABCDEF+GHIJ.klmz" > > -- > Aur�lien AMILINThanks guyz, this worked like a charm.. Thanks to all of you who replies so quickly .. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.