Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Weird Regexes Stuff

by slinky773 (Sexton)
on Aug 18, 2011 at 16:46 UTC ( [id://921012]=perlquestion: print w/replies, xml ) Need Help??

slinky773 has asked for the wisdom of the Perl Monks concerning the following question:

I was going through Sam's Teach Yourself Perl in 24 hours and I got to the end of hour 6, the regex section, and I see this activity:
See whether you can produce a pattern to match a standard time format. + All the following should be acceptable: 12:00am, 5:00pm, 8:30AM. These sho +uld probably not be accepted: 3:00,2:60am, 99:00am, 3:0pm.
OK, easy enough:
#!/usr/bin/perl -w use strict; use warnings; print "Type in the current time!\n"; my $input = <STDIN>; while($input) { if($input =~ m/(\d)*(\d):(\d)(\d)(\w)(\w)/ | $input =~ m/(\d)*(\d) +:(\d)(\d)(\s)(\w)(\w)/) { if($1 > 1 || $2 > 2 || $3 > 6) { print "That is not a valid time!\n"; print "Type the time again.\n"; $input = <STDIN>; } else { print "That is a valid time!\n"; $input = 0; } } else { print "Please be more specific.\n"; $input = <STDIN>; } }
However, this is the terminal output:
Last login: Thu Aug 18 11:28:44 on ttys000 youngs-mac-mini:~ fenimore$ cd ~/Documents youngs-mac-mini:Documents fenimore$ perl TimeChecker.pl Type in the current time! 4:90 Please be more specific. 4:30AM Use of uninitialized value $1 in numeric gt (>) at TimeChecker.pl line + 13, <STDIN> line 2. That is not a valid time! Type the time again. 12:30PM That is a valid time! youngs-mac-mini:Documents fenimore$
Er... any help here? For added clarity, this is line 13:
if($1 > 1 || $2 > 2 || $3 > 6)

Replies are listed 'Best First'.
Re: Weird Regexes Stuff
by toolic (Bishop) on Aug 18, 2011 at 17:08 UTC
    (\d)*(\d)
    Try changing that to
    (\d{1,2})
    but you'll then have to adjust your $1,$2,$3. See perlre
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Weird Regexes Stuff
by Samy_rio (Vicar) on Aug 18, 2011 at 18:00 UTC

    Updated

    this may help you

    #!/usr/bin/perl -w use strict; use warnings; my $input = 'All the following should be acceptable: 12:00am, 5:00pm, +8:30AM. These should probably not be accepted: 3:00,2:60am, 99:00am, +3:0pm, 29:00AM.'; while ($input =~ m/\W([1-9]|[01][0-2]|2[0-3]):[0-5][0-9][ap]m/gsi) { print "This is a valid time!\t$&\n"; } __END__ OUTPUT This is a valid time! 12:00am This is a valid time! 5:00pm This is a valid time! 8:30AM

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      >perl -wMstrict -le "my $input = '12:00AM 29:00AM'; while ($input =~ m/\W(0|[0-2]?[0-9]):[0-5][0-9][ap]m/gsi) { print qq{This is a valid time!\t'$&'}; } " This is a valid time! ' 29:00AM'

      The updated solution still has a problem, IMO, with that pesky  \W anchoring the start of the match. Is the following really what you intend?

      >perl -wMstrict -le "my $input = '12:00am, 5:00pm,8:30AM'; while ($input =~ m/\W([1-9]|[01][0-2]|2[0-3]):[0-5][0-9][ap]m/gsi) { print qq{This is a valid time!\t$&}; } " This is a valid time! 5:00pm This is a valid time! ,8:30AM

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://921012]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found