Guest_imported
New member
- Jan 1, 1970
- 0
I'm slightly inexperienced with perl regexp and so preg_match is giving me a little trouble. Here's what I want to do:
My function is getting a number that represents a salary, so it can come in formatted like: $40,000 , 40,000 , 40000, or the same three with .00 at the end. I want preg_match to be true if the salary conforms to the format, false if it doesn't.
This is what I got up to now:
preg_match("/[$]?[0-9]+[,]?[0-9][0-9][0-9]/",$salary)
now the problem is
#1. For some reason it accepts an infinite amount of $ at the beginning of the salary, even though [$] has a ? attached to it.
#2. Missing the catch for having .xx (xx begin a number) at the end. I tried adding [\.\d\d]? or [\.0-90-9]? or similar at the end of the regexp, but it didn't work. It just ends up matching any text you add to the salary number at the end.
Any help would be appreciated!
My function is getting a number that represents a salary, so it can come in formatted like: $40,000 , 40,000 , 40000, or the same three with .00 at the end. I want preg_match to be true if the salary conforms to the format, false if it doesn't.
This is what I got up to now:
preg_match("/[$]?[0-9]+[,]?[0-9][0-9][0-9]/",$salary)
now the problem is
#1. For some reason it accepts an infinite amount of $ at the beginning of the salary, even though [$] has a ? attached to it.
#2. Missing the catch for having .xx (xx begin a number) at the end. I tried adding [\.\d\d]? or [\.0-90-9]? or similar at the end of the regexp, but it didn't work. It just ends up matching any text you add to the salary number at the end.
Any help would be appreciated!