# Regular Expression Question

**URL:** <https://www.ruby-forum.com/t/regular-expression-question/53532>\
**Category:** Rails\
**Created:** [February 8, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532 "2006-02-08T19:00:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/1 "2006-02-08T19:00:44Z")

</div>

I have looked through the Ruby Pickaxe book as well as done a search  
but can’t figure out how to validate the format of a field. The field  
I want to validate is a credit card expiration date field. Based on  
our user study (going around the office and asking everyone the way  
they like to fill out that particular field type) everyone prefers  
the following format in one field:

MM/YY

They do not like to have to tab or to use dropdowns. I have tried  
several iterations but I can’t figure out the “/” I have tried:

%r{/(\d+)(/)(\d+)/} (that is not a V it is \ /)

as well as:

/(\d+)(/)(\d+)/

and neither work. Is it possible to look for a / in a regexp? If not  
I guess I will break it out into two fields but would rather please  
the majority of users than force them to do something because the  
language doesn’t allow me to do what I want to do.

Thanks

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/2 "2006-02-08T19:14:28Z")

</div>

Andrew F. wrote:

> several iterations but I can’t figure out the “/” I have tried:
> 
> %r{/(\d+)(/)(\d+)/} (that is not a V it is \ /)
> 
> as well as:
> 
> /(\d+)(/)(\d+)/
> 
> and neither work. Is it possible to look for a / in a regexp?

Hmm… this worked just fine for me:

[Pendelhaven] ~: irb  
irb(main):001:0\> a = “11/22”  
=\> “11/22”  
irb(main):002:0\> /(\d+)/(\d+)/ === a  
=\> true  
irb(main):003:0\> quit

How are you trying to verify the regex against the field value?

Btw, unless you are planning to use the back-references that you’ve got  
in your  
pattern, which I doubt you are for a validation match, you can drop the  
()s:

```
 /\d+\/\d+/ === a

```

Works just fine and doesn’t waste memory creating back-references…

-Brian

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/3 "2006-02-08T19:22:56Z")

</div>

Figured out my issue. had a typo in my code.

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/4 "2006-02-08T20:57:12Z")

</div>

Also bear in mind that this is going to match one or more digits  
followed by a slash followed by one or more digits…  
so it’ll match 123123/2134523, etc. And even if you restrict the form  
field to five chars, they can still enter 99/99.

b

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/5 "2006-02-08T22:08:29Z")

</div>

So if you KNOW that you’re dealing with MM/YY, you can at least  
use: %r{(?:0[1-9]|1[012])/\d\d}

irb(main):004:0\> a = %r{(?:0[1-9]|1[012])/\d\d}  
=\> /(?:0[1-9]|1[012])/\d\d/  
irb(main):005:0\> a === “12/07”  
=\> true  
irb(main):006:0\> a === “92/08”  
=\> false  
irb(main):007:0\> a === “123/4”  
=\> false  
irb(main):008:0\> a === “13/06”  
=\> false

You could get tricky and include basic checks on the year, too:  
%r{(?:0[1-9]|1[012])/(?:0[6-9]|1[0-5])}  
If you assume an expiration will be between now and 2015. (Of  
course, you could get crazy with this and generate an RE that would  
reject “01/06” since that’s already passed, but if you’re just  
catching blatant typos, the first RE will be fine.

-Rob

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [February 8, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/regular-expression-question/53532/6 "2006-02-08T21:46:39Z")

</div>

I’m still pretty slow at regexes… but I think this will do what you  
want:

/^([0]?[1-9]|[1][0-2])/[0-9]{2}$/

b
