Re: Help with a regular expression for 0, 1 or 2 decimal places

From: Date: Tue, 25 Oct 2005 02:08:37 +0000
Subject: Re: Help with a regular expression for 0, 1 or 2 decimal places
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-224599@lists.php.net to get a copy of this message
On Mon, October 24, 2005 3:59 pm, Shaun wrote: > I am trying to create a regular expression for a width of a room, the > value > can be a whole integer (up to 999) with up to 2 decimal places -when > it is > stored in the database mysql will pad the value accordingly. > > /^[0-9]{1,3}.?[0-9]{0,2}?$/ Because '.' is special to preg, this expression ALSO matches: 12a25 5z43 7a [aside] Engineer: 7A, please. Davey: What number, please? Band: 7A! Davey: All right, okay, just cuz I'm short [/aside] This might (or might not) do it: /^[0-9]{1,3}(\\.[0-9]{1,2})?$/ The \\ is there so that PHP will turn it into \, and then you have \. in the regular expression which the PRCE will interpret as "the literal character known as 'dot'" instead of "any old character at all" > Also is there any difference between [0-9] and \d Try it and see. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#224599) next »