# Date-building regexes

**URL:** <https://www.ruby-forum.com/t/date-building-regexes/58827>\
**Category:** Ruby\
**Created:** [April 26, 2006, 11:19am UTC](https://www.ruby-forum.com/t/date-building-regexes/58827 "2006-04-26T11:19:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 26, 2006, 11:19am UTC](https://www.ruby-forum.com/t/date-building-regexes/58827/1 "2006-04-26T11:19:29Z")

</div>

Hi

Does anyone know of a regular expression for _building_ a date? That is  
one  
that will match “3”, “30”, “30/”, “30/0” (etc), but not “30/099” or  
(ideally) “30/02”? I’ve had a quick look into it and it seems pretty  
complex, and I figured someone must have solved the problem before.

Thanks  
Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 26, 2006, 11:41am UTC](https://www.ruby-forum.com/t/date-building-regexes/58827/2 "2006-04-26T11:41:13Z")

</div>

2006/4/26, Ashley M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi
> 
> Does anyone know of a regular expression for _building_ a date? That is one  
> that will match “3”, “30”, “30/”, “30/0” (etc), but not “30/099” or  
> (ideally) “30/02”? I’ve had a quick look into it and it seems pretty  
> complex, and I figured someone must have solved the problem before.

Not 100% sure but it might be more efficient to just implement the  
automata that matches the pattern and feed events (i.e. chars) to it  
while the user enters them. Otherwise your regexp will grow pretty  
complex as you have to provide options / alternatives for every  
possible path the pattern can take. HTH

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 26, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/date-building-regexes/58827/3 "2006-04-26T16:35:48Z")

</div>

On Wed, 26 Apr 2006, Ashley M. wrote:

> Hi
> 
> Does anyone know of a regular expression for _building_ a date? That is one  
> that will match “3”, “30”, “30/”, “30/0” (etc), but not “30/099” or  
> (ideally) “30/02”? I’ve had a quick look into it and it seems pretty  
> complex, and I figured someone must have solved the problem before.
> 
> Thanks  
> Ashley

not regexes - but this may solve your problem.

[http://runt.rubyforge.org/](http://runt.rubyforge.org/)

-a

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 29, 2006, 1:30am UTC](https://www.ruby-forum.com/t/date-building-regexes/58827/4 "2006-04-29T01:30:55Z")

</div>

Here’s what you can do:

mydate = ‘2/4/2006’  
match = /\b(0?[1-9]|1[012])- /.-  
/.?[0-9]{2}\b/.match(mydate)

On Wed, 26 Apr 2006, Robert K. wrote:

> while the user enters them. Otherwise your regexp will grow pretty

–Craig K.  
Octo Performance Engineering, Inc

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 26, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/date-building-regexes/58827/5 "2006-04-26T22:19:20Z")

</div>

Ashley M. wrote:

> 

I’m not sure exactly what you mean by ‘building’ a date, but have you  
looked at Date::parse? Does that do what you want?

[http://ruby-doc.org/core/classes/Date.html#M001576](http://ruby-doc.org/core/classes/Date.html#M001576)

-Justin
