Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match troubles

Status
Not open for further replies.

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!
 
For the tests i made, i found this

preg_match("/^[\$]?\d+([,]\d+)?([\.]\d{2})?$/",$number)

Hope it helps you out.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thank you!!!!! It worked like a charm.

In hopes to learn a bit more about regexps, could I ask you why it has the ^ at the beginning?

Thanks again,
Luis
 
When the ^ is the first character of a regexp, it means that it should only match the very beginning of the string. //Daniel
 
^ means beggining of the string
$ means end of string (that's why i put a backslash when i was meaning the $ sign in the beginning
+ means 1 or more times
* means zero or more times
? means zero or one time

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top