FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Perl regex help





a_dubDesign
I'm a perl noob, and need some help figuring out the regex for time in hh:mmAM/PM kind of deal, and any single digit hour doesn't need a leading 0. It seems like it would be simple, but I just can't get it, and its driving me nuts!
santium
I recommend the site www.Regular-Expressions.info because they have great tutorials and plenty of examples on how regexs work. There is also a "cheat-sheet" on how to do simple things like what you are asking. The reason I won't tell you straight out is because it'll be better to go through and find out how regexs work and what is being done, etc.
a_dubDesign
thanks for the link and all, but not all that useful. Maybe I should be more specific. I know about all the operations and whatnot, but since this is my first real experience with regexs I need a little help getting everything to click together. The only issue I'm really having is getting the hours worked out so there doesn't have to be a leading 0 for the single digit hours.

1[0-2] works great for the double digit hours
[1-9] works out great for the single
then I try alternation doing (1[0-2]|[1-9]) but it gives me everything from 1-19, which is the closest I've come to it so far. I've moved stuff around, and tried some other stuff out of mass desperation, but haven't gotten anything yet.

Here's the whole thing in case there's other suggestions anyone could make to clean it up.
m/(1[0-2]|[1-9]):[0-5][0-9](am|pm)/i
hexkid
Code:
/^(?:1|2|3|4|5|6|7|8|9|10|11|12):[0-5][0-9](?:a|p)m$/i
Kaneda
Or this:

Code:
/^(?:(?:1[0-2])|[0-9]):[0-5][0-9](?:am|pm)$/i


... this assuming you don't want to capture the parts, otherwise:
Code:
/^((?:1[0-2])|[0-9]):([0-5][0-9])(am|pm)$/i


Which captures hours into backreference 1, minutes into 2 and pm/am into 3.
hexkid
Kaneda wrote:
Code:
/^(?:(?:1[0-2])|[0-9]):[0-5][0-9](?:am|pm)$/i

Is "0:42pm" a valid time? Smile
a_dubDesign
you guys are awesome.

Code:
/^((?:1[0-2])|[0-9]):([0-5][0-9])(am|pm)$/i

couple of questions if you don't mind. Whats the function of the ?: in the hours section? I thought the ? metacharacter matched the preceding character 0or1 times, but there isn't a preceding character. Does that turn it into any character 0 or 1 times? Or does ?: do something I've not gotten to in the book/website I've checked out?

again, you guys are awesome.
Kaneda
hexkid wrote:
Kaneda wrote:
Code:
/^(?:(?:1[0-2])|[0-9]):[0-5][0-9](?:am|pm)$/i

Is "0:42pm" a valid time? Smile


We don't use AM/PM in continental Europe, so at the time it seemed natural to me - at least "0:42am" Wink

OK then:
Code:
/^(?:(?:1[0-2])|[1-9]):[0-5][0-9](?:am|pm)$/i



a_dubDesign wrote:
couple of questions if you don't mind. Whats the function of the ?: in the hours section? I thought the ? metacharacter matched the preceding character 0or1 times, but there isn't a preceding character. Does that turn it into any character 0 or 1 times? Or does ?: do something I've not gotten to in the book/website I've checked out?


Probably the latter. Parantheses do two things in regular expressions (which is more confusing than helpful): They make a "group" in an expression (somewhat like the parantheses in math), and they capture subpatterns. In this case, we need some groups (one of them is actually redundant, but nevermind that), but we don't really want all these groups to also be captured. This:
Code:
/^((1[0-2])|[1-9]):([0-5][0-9])(am|pm)$/i


... for the time "11:42pm" would give the following suppattern matches:
11 - for (1[0-2]))
11 - for ((1[0-2])|[1-9])
42 - for ([0-5][0-9]))
pm - for (am|pm))

To just use parantheses as an expression grouping, without capturing the subpattern, we use "?:" right after the opening paranthesis. This is a feature which is in no way related to the usual "?" quantifier feature. So to avoid capturing the bit we don't want:

Code:
/^((?:1[0-2])|[1-9]):([0-5][0-9])(am|pm)$/i
a_dubDesign
you are the man kaneda. That definetly wasn't in the book I'm going through.
Related topics
Perl server referencing
writing COM components in Perl
cgi-bin help
executing a perl script
PHP Regex
Need help with a menu!
Anyone that can give me help/suggestions for forms?
Help, I need LWP::Simple perl module!!
Scripting Language
perl help... script almost working... please help..
[ HELP] Problems with PHP and Perl
Sending simple emails using PERL - Help required
a regex help
About Adding Text to Beginning of File
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.