Regular expression help needed 
Author Message
 Regular expression help needed

Hi,
I'm having a little trouble with a regexp. I need to write.

Basically I'm looking for something which allows time inputs following these
rules:

Minutes - either a single 0 or a positive digit but without leading zeroes
Seconds - between 00 and 59, of course - must always be two digits
Fractional - two OR three digits

Allowed formats:
0:53.995
1:32.94
14:32.095

NOT allowed:
01:3.50
00:78.4832

Does anyone have a good regexp which could do this - reformatting erroneous
input if possible?

Thanks in advance. :)

--
Michael Price



Mon, 01 Aug 2005 02:47:18 GMT  
 Regular expression help needed

Quote:

> Basically I'm looking for something which allows time inputs following
> these rules:

> Minutes - either a single 0 or a positive digit but without leading zeroes
> Seconds - between 00 and 59, of course - must always be two digits
> Fractional - two OR three digits

This should do the trick (untested!)

^(0|[1-9]\d*):[0-5]\d\.\d{2,3}$

Quote:
> Does anyone have a good regexp which could do this - reformatting
> erroneous input if possible?

I wouldn't recommend reformatting erronous input, this will only be a source
of problems because it's both very hard to do, and there is a pretty good
change that the reformatting doesn't yield what the user intended.

Andr N?ss



Mon, 01 Aug 2005 03:04:24 GMT  
 Regular expression help needed

Quote:
> Minutes - either a single 0 or a positive digit but without leading zeroes
> Seconds - between 00 and 59, of course - must always be two digits
> Fractional - two OR three digits
> Allowed formats:
> 0:53.995
> 1:32.94
> 14:32.095
> NOT allowed:
> 01:3.50
> 00:78.4832

First replace the : by a .
    $var = eregi_replace(":", ".", $var);

Now, check for valid minute/second/fraction parts:
minutes:  (0?[1-9]|[1-9][0-9])
seconds:  ([0-5]?[0-9])

etc...

--

experience the warmth :: before you grow                     -- incubus



Mon, 01 Aug 2005 03:07:28 GMT  
 Regular expression help needed

Quote:
> > Minutes - either a single 0 or a positive digit but without leading
zeroes
> > Seconds - between 00 and 59, of course - must always be two digits
> > Fractional - two OR three digits

> This should do the trick (untested!)

> ^(0|[1-9]\d*):[0-5]\d\.\d{2,3}$

Cheers, I'll give it a shot. :)

Quote:
> > Does anyone have a good regexp which could do this - reformatting
> > erroneous input if possible?

> I wouldn't recommend reformatting erronous input, this will only be a
source
> of problems because it's both very hard to do, and there is a pretty good
> change that the reformatting doesn't yield what the user intended.

Perhaps best to simply print an error message then?

--
Michael Price



Mon, 01 Aug 2005 03:07:42 GMT  
 Regular expression help needed

Quote:

>> I wouldn't recommend reformatting erronous input, this will only be a
> source
>> of problems because it's both very hard to do, and there is a pretty good
>> change that the reformatting doesn't yield what the user intended.

> Perhaps best to simply print an error message then?

Yes. That way the user hopefully learns to do things properly.

Andr N?ss



Mon, 01 Aug 2005 03:08:56 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Regular expression help needed

2. Regular Expression Help Needed

3. Regular Expression Help Needed

4. Need help with regular expression

5. Regular Expression newbie needs help

6. Need help with regular expression / regsub

7. need help on COMPLEX regular expression problem using Expect

8. need help with with expect's regular expressions

9. Simple Regular Expression Needed...

10. Need code to match strings with regular expressions

11. Need a guide about regular expressions

12. Regular Expression help

 

 
Powered by phpBB® Forum Software