Sandip Ransing
2009-Sep-11 11:36 UTC
regular expression matching date, author and agency in string
Hello All, I wanted regular expression for the following patterns like Nauzer Bharucha, TNN 10 September 2009, 12:01am IST IANS 3 September 2009, 02:57pm IST regular expression should return *date* in first group *author* in second group and *agency* in third group for below pattern it should return Nauzer Bharucha, TNN 10 September 2009, 12:01am IST $1 = 10 September 2009 $2 = Nauzer Bharucha $3 = TNN In second case author is nil Any ideas ? Sandip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Abhinav Saxena
2009-Sep-11 12:06 UTC
Re: regular expression matching date, author and agency in string
Not to sound rude, but you should do it yourself. It will help you in your programming career. Moreover, it''s not a rails related issue. You can use Internet search for learning more about regex. This is first result I got on Google: http://www.regular-expressions.info/reference.html Thanks, Abhinav PS: Please don''t outsource your work to this mailing list, and if you do, please do tell us what all you tried. -- अभिनव http://twitter.com/abhinav On Fri, Sep 11, 2009 at 5:06 PM, Sandip Ransing <san2821-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello All, > > I wanted regular expression for the following patterns like > > Nauzer Bharucha, TNN 10 September 2009, 12:01am IST > > IANS 3 September 2009, 02:57pm IST > > regular expression should return > *date* in first group > *author* in second group > and *agency* in third group > > for below pattern it should return > Nauzer Bharucha, TNN 10 September 2009, 12:01am IST > > $1 = 10 September 2009 > $2 = Nauzer Bharucha > $3 = TNN > > In second case author is nil > > Any ideas ? > > Sandip > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black
2009-Sep-11 12:09 UTC
Re: regular expression matching date, author and agency in string
Hi -- On Fri, 11 Sep 2009, Sandip Ransing wrote:> Hello All, > > I wanted regular expression for the following patterns like > > Nauzer Bharucha, TNN 10 September 2009, 12:01am IST > > IANS 3 September 2009, 02:57pm IST > > regular expression should return > date in first group > author in second group > and agency in third group > > for below pattern it should return > Nauzer Bharucha, TNN 10 September 2009, 12:01am IST > > $1 = 10 September 2009 > $2 = Nauzer Bharucha > $3 = TNN > > In second case author is nil > > Any ideas ?I''m in a "teaching to fish" mood :-) If you''re not yet thinking in regex terms, you need to dissect and describe your pattern, and then translate it into a regular expression. Here''s a description, as I understand it. zero or one of (one or more non-comma characters) a comma one or more spaces (three uppercase letters) one or more spaces (one or more non-comma characters) etc. Also, note that you can''t choose which capture will be $1, $2, etc. They are assigned strictly in the order in which the left parentheses appear, left to right (unless you use (?:) to suppress inclusion of a particular group). David -- David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com Ruby/Rails training, mentoring, consulting, code-review Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2)