|
Prev: FAQ 2.6 What modules and extensions are available for Perl? What is CPAN? What does CPAN/src/... mean?
Next: Help with Code
From: joez311 on 28 Sep 2006 09:29 Hi, Can someone help me out with the following pattern match? I want to match on the date went its formated like mm/dd/yy. I was trying: /[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/ but my match doesn't seam to work as expected. I think its because of the / used to split the feilds. Could some one show me the correct way to pattern match on this? Thanks, Zim
From: David Squire on 28 Sep 2006 09:40 joez311(a)yahoo.com wrote: > Hi, > Can someone help me out with the following pattern match? I want to > match on the date went its formated like mm/dd/yy. I was trying: > /[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/ > but my match doesn't seam to work as expected. I think its because of > the / used to split the feilds. Could some one show me the correct way > to pattern match on this? Hmmm. Very hard to know exactly what you want without a real Perl code example (please read the posting guidelines for this group). Perhaps you want something like: my ($month, $day, $year) = ($date_string =~ m|([01][0-9])/([0-3][0-9])/([0-9][0-9])|); # Untested. Assumes there will always be two characters per field Note that you can use delimiters other than '/' for your regular expression if you start with 'm' explicity. There are also several powerful modules on CPAN for parsing dates and times. It would be a good idea to use one of those for any serious work. DS
From: Dr.Ruud on 28 Sep 2006 10:01 joez311(a)yahoo.com schreef: > Can someone help me out with the following pattern match? I want to > match on the date went its formated like mm/dd/yy. I was trying: > /[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/ > but my match doesn't seam to work as expected. I think its because of > the / used to split the feilds. Could some one show me the correct way > to pattern match on this? > Thanks, > Zim I see a comma that probably shouldn't be there, and a missing "]", and unescaped slashes, which leads to: m~[01][0-9]/[0-3][0-9]/[0-9][0-9]~ Check out Regexp::Common http://search.cpan.org/search?module=Regexp::Common -- Affijn, Ruud "Gewoon is een tijger."
From: Paul Lalli on 28 Sep 2006 14:03 joez...(a)yahoo.com wrote: > Can someone help me out with the following pattern match? I want to > match on the date went its formated like mm/dd/yy. I was trying: > /[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/ > but my match doesn't seam to work as expected. I think its because of > the / used to split the feilds. You have multiple problems. 1) if you use / as the delimiter, you have to escape (that is, precede with a backslash) any actual / characters that you want to match. 2) commas match themselves in character classes. A character class is either a list (with nothing separating the elements) or a range (using the - character) or a combination of both 3) Not all of your character classes are terminated 4) The character class [0-9] is more easily written \d Putting those together: m/[01]\d\/[0-3]\d\/\d\d/; However, this is still not sufficient to match a real date, as it would allow a date of, for example, 19/38/04; > Could some one show me the correct way > to pattern match on this? The correct way is to not reinvent the wheel: use Regexp::Common qw/time/; /$RE{time}{mdy}/; to allow any month-day-year pattern, or /$RE{time}{m2d2y2}/ to specify exactly two digits for each field. Paul Lalli
From: usenet on 28 Sep 2006 14:15
joez...(a)yahoo.com wrote: > match on the date went its formated like mm/dd/yy. I was trying: > /[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/ > but my match doesn't seam to work as expected. I think its because of > the / used to split the feilds. Since you use "/" to delimit your pattern, Perl finds the next "/" and thinks the pattern specification has ended. You can use another character to delimit the match, but you must actually put "m" in front of it, such as using bangs: m!\d\d/[0-3]\d/\d\d!; Or you can escape the embedded slashes: /d\d\/[0-3]\d\/\d\d/; Or you can use Regexp::Common from CPAN... BTW, you didn't close your second-to-last character class. -- David Filmer (http://DavidFilmer.com) |