Question about regex and date formatting

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?

···

--
Posted via http://www.ruby-forum.com/.

Älphä Blüë wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

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

returns => 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:

m=/\d{4}-\d{2}-\d{2}/.match(date)

=> #<MatchData:0x573348>

pp m

#<MatchData "2009-06-01">

Also this little change should make it obvious what happens:

date = "blablabla2009-06-01"

=> "blablabla2009-06-01"

date =~ /\d{4}-\d{2}-\d{2}$/

=> 9

HTH,
Michael

···

On Wed, Jul 1, 2009 at 6:21 PM, Älphä Blüë<jdezenzio@gmail.com> wrote:

--
Il pinguino ha rubato il mio lanciafiamme.

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

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

···

--
Posted via http://www.ruby-forum.com/.