regex problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
sh33p1985
Forum Commoner
Posts: 78
Joined: Thu Mar 11, 2004 9:22 am

regex problem

Post by sh33p1985 »

ok, i confess, regex is my achillies heel! ive been getting better at it but ive become a bit unstuck in my aim to validate certain fields in some form data.

basically i was trying to verify a price field, only allowing the format:

optional £
one or more digits
"."
2 digits

e.g. £1.00, 1.00 ok
1.0 not ok

i got as far as verifying the x.xx part of the string but im not entirely happy with the code as 1.11111 matches because 1.11 appears in the string as a substirng.

also, another simliar field i have needs to accept numeric values in this form:

25 or 25.0 or 25.00

any help would be much appreciated.

thanks
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

$pattern = "/^(£)?(\d*)(\.(\d\d))?$/";
will accept: £25, £25.00, 25, 25.00

Code: Select all

$pattern = "/^(\d*)(\.(\d){1,2})?$/";
will accept: 25, 25.0, 25.00

Cheers,
Kieran
Post Reply