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?
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?
Ä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
On Wed, Jul 1, 2009 at 6:21 PM, Älphä Blüë[email protected] wrote:
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
–
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…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs