regex experts? help please

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-08-2006
brendan
 
Posts: n/a
Default regex experts? help please

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



Reply With Quote
  #2 (permalink)  
Old 02-08-2006
David Haynes
 
Posts: n/a
Default Re: regex experts? help please

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-

Reply With Quote
  #3 (permalink)  
Old 02-08-2006
Ben Bacarisse
 
Posts: n/a
Default Re: regex experts? help please

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.

Reply With Quote
  #4 (permalink)  
Old 02-09-2006
rlee0001
 
Posts: n/a
Default Re: regex experts? help please

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

Reply With Quote
  #5 (permalink)  
Old 02-09-2006
brendan
 
Posts: n/a
Default Re: regex experts? help please


"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


Reply With Quote
  #6 (permalink)  
Old 02-09-2006
Ben Bacarisse
 
Posts: n/a
Default Re: regex experts? help please

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.

Reply With Quote
  #7 (permalink)  
Old 02-09-2006
rlee0001
 
Posts: n/a
Default Re: regex experts? help please

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

Reply With Quote
  #8 (permalink)  
Old 02-11-2006
Ben Bacarisse
 
Posts: n/a
Default Re: regex experts? help please

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.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:09 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0