Question about regex and date formatting

  • Thread starter Älphä Blüë
  • Start date
Ã

Älphä Blüë

I'm trying to check whether yyyy-dd-mm in a string or date is true.

I've tried:

date = "2009-06-01"
date =~ /^\d{4}-\d{2}-\d{2}$/

returns => 0

It should return true. What am I missing here?
 
G

Greg Willits

Älphä Blüë said:
I'm trying to check whether yyyy-dd-mm in a string or date is true.

I've tried:

date = "2009-06-01"
date =~ /^\d{4}-\d{2}-\d{2}$/

returns => 0

It should return true. What am I missing here?

That's the correct response. =~ return the starting position of the
string that was matched. If there is no match it returns nil.

-- gw
 
M

Michael Kohl

date =3D "2009-06-01"
date =3D~ /^\d{4}-\d{2}-\d{2}$/

returns =3D> 0

Remember that 0 in Ruby is not false, only 'nil' and false are. 0 is
the index of where in your string your match starts. Your RegEx is ok:
#<MatchData "2009-06-01">

Also this little change should make it obvious what happens:
=3D> 9

HTH,
Michael

--=20
Il pinguino ha rubato il mio lanciafiamme.

Blog: http://citizen428.net/
Twitter: http://twitter.com/citizen428
 
Ã

Älphä Blüë

Ah I didn't realize that. I thought 0 meant false. I appreciate the
answer and the tips..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top