This is a discussion on regex experts? help please within the alt.comp.lang.php forums, part of the PHP Programming Forums category; could someone clarify why this won't work? I'm trying to collect the times from a string regardless of ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
could someone clarify why this won't work?
I'm trying to collect the times from a string regardless of how they're entered (ie 9pm 9:30pm 9:00 ) using $timeMatch="/[0-9]{1,2}?[ *]?[.|:|pm|am|noon]{1,4}[ *]?[[0-9]{2}[ *]?[pm|am|noon]{0,4}]?]??/"; preg_match_all($timeMatch,trim(strtolower($txt)),$ times, PREG_PATTERN_ORDER) THIS WORKS $txt="the conference will be held from 5:30 pm to 9:30pm outputs $times[0][0]="5:30 pm"; $times[0][1] ="9:30pm"; however this doesn't (but should!?!) $txt="9pm"; regex doesn't fire! anyone? cheers |
|
|||
|
brendan wrote:
> could someone clarify why this won't work? > > I'm trying to collect the times from a string regardless of how they're > entered (ie 9pm 9:30pm 9:00 ) > using > > $timeMatch="/[0-9]{1,2}?[ *]?[.|:|pm|am|noon]{1,4}[ *]?[[0-9]{2}[ > *]?[pm|am|noon]{0,4}]?]??/"; > preg_match_all($timeMatch,trim(strtolower($txt)),$ times, PREG_PATTERN_ORDER) > > THIS WORKS > > $txt="the conference will be held from 5:30 pm to 9:30pm > outputs $times[0][0]="5:30 pm"; $times[0][1] ="9:30pm"; > > however this doesn't (but should!?!) > $txt="9pm"; > regex doesn't fire! > > anyone? > > cheers An alternate example: <?php $test = array('9pm', '9:30pm', '9:00'); foreach( $test as $t ) { $time = strtotime($t); $new_time = date('H:i:s', $time); echo "input = $t, time = $time, new_time = $new_time\n"; } ?> -david- |
|
|||
|
On Wed, 08 Feb 2006 15:49:25 +0000, brendan wrote:
> could someone clarify why this won't work? > > I'm trying to collect the times from a string regardless of how they're > entered (ie 9pm 9:30pm 9:00 ) > using > > $timeMatch="/[0-9]{1,2}?[ *]?[.|:|pm|am|noon]{1,4}[ *]?[[0-9]{2}[ > *]?[pm|am|noon]{0,4}]?]??/"; You have the syntax wrong. It looks like you think [] introduces options. It introduces a character class so [.|:pm|am|noon] == [.|:pmano] and matches any one of the characters inside. Also [0-9]{1,2}? is the same as [0-9]{,2}. Matching zero or more with * means you don't need to make that part optional with ?. Try: $timeMatch='/\d{1,2}(:\d{2})? *(noon|am|pm)?/'; as one simple option. \d means "any digit". -- Ben. |
|
|||
|
You should probably point out that your expression with match simple
numbers such as '1', '12', '76', '45', and so on. It will also match any trailing space along with those numbers. For example, '69 ' might get matched. The space should be allowed only when noon/am/pm is encountered. If one of these are not encountered than the :\d{2} should be required. This should be a simple fix. -Robert |
|
|||
|
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message news:pan.2006.02.08.21.22.33.374481@bsb.me.uk... > On Wed, 08 Feb 2006 15:49:25 +0000, brendan wrote: > Try: > > $timeMatch='/\d{1,2}(:\d{2})? *(noon|am|pm)?/'; > > as one simple option. \d means "any digit". thanks Ben, very helpful! ta brendan |
|
|||
|
On Wed, 08 Feb 2006 21:04:31 -0800, rlee0001 wrote:
> You should probably point out that your expression with match simple > numbers such as '1', '12', '76', '45', and so on. Why should I? What the expression matches is entirely unambiguous and what the OP intended is not (e.g. does "I can't got to the meeting at 3" contain a time or not?). Matching only valid times would make the regex hopelessly complex. > This should be a simple fix. Whould it not be more helpful to give the fix? I certainly would be helpful if you included quoted text and attribution in your reply. Google groups can do this. -- Ben. |
|
|||
|
Ben,
Google groups can also show the entire thread on one page. In fact that is its default behaviour. Quoted text is only appropriate if you want to respond to multiple specific points from the original message. I made the subject of my message clear enough as to clear up any context ambiguities about what parts of your message I am replying to. Anyways, the only reason I posted was to make brendan aware that your regexp matched numbers since this can lead to false positives in some instances depending on his application and needs. I wasn't criticizing you and fully understand that numbers can be used as times in text. Hell I wasn't even trying to correct you. I was just elaborating for brendan's sake in case he didn't realize what your regexp might match. Also, I don't think either of us should always have to hand over a perfect working solution to a question. I think your original regexp does a great job of leading brendan in the right direction and providing a possible solution. But I think that ultimately the responsiblity rests with brendan to ensure that the solution provided will work for his needs and to modify that solution if nessisary. Just by pointing out to brendan that your regexp will match numbers I've enabled him to decide whether to use that provided regexp as is or (god forbid) actually try to modify it himself to fit his specific needs. Besides, if he needed a modified version that didn't match simple numbers and he couldn't figure it out on his own he could always ask. I'd be wasting my time posting a solution to a problem that nobody has. Don't take things so personally. We're on the same team remember? :o) -Robert |
|
|||
|
On Thu, 09 Feb 2006 10:55:03 -0800, rlee0001 wrote:
> Google groups can also show the entire thread on one page. In fact that is > its default behaviour. Quoted text is only appropriate if you want to > respond to multiple specific points from the original message. I can only suggest, as strongly as I can, that follow accepted Usenet posting gudelines. They were devised to make the medium universally useful. > Don't take things so personally. We're on the same team remember? :o) I did not take it personally. There was nothing at all personal in your reply. If you had quoted my message and elabrated on it (explaining it, enhancing it) I would have said nothing (except maybe "thanks!") but instead you told me what I should have done. I got annoyed by being told what I should have done, that is all. -- Ben. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|