# Regular expression matching date, author and agency in string

**URL:** <https://www.ruby-forum.com/t/regular-expression-matching-date-author-and-agency-in-string/175338>\
**Category:** Rails\
**Created:** [September 11, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/regular-expression-matching-date-author-and-agency-in-string/175338 "2009-09-11T13:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![san2821](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@san2821](https://www.ruby-forum.com/u/san2821)\
**Post date:** [September 11, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/regular-expression-matching-date-author-and-agency-in-string/175338/1 "2009-09-11T13:38:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![san2821](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@san2821](https://www.ruby-forum.com/u/san2821)\
**Post date:** [September 11, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/regular-expression-matching-date-author-and-agency-in-string/175338/2 "2009-09-11T14:08:32Z")

</div>

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:

> **[Regular Expressions Reference](https://www.regular-expressions.info/reference.html)**
>
> Reference of the various syntactic elements that can appear in regular expressions

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](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![san2821](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@san2821](https://www.ruby-forum.com/u/san2821)\
**Post date:** [September 11, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/regular-expression-matching-date-author-and-agency-in-string/175338/3 "2009-09-11T14:11:28Z")

</div>

Hi –

On Fri, 11 Sep 2009, Sandip R. wrote:

> author in second group
> 
> 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](http://www.rubypal.com)  
Ruby/Rails training, mentoring, consulting, code-review  
Latest book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))
