?
Solved

Need some help with an regex

Posted on 2005-05-01
3
Medium Priority
?
198 Views
Last Modified: 2006-11-18
ok for my program i have an discoutn field.  The discount can be an amount or a percent.

I wrote these regex

// Figures out if it's it's an valid amount
 '^[0-9]*(\.[0-9]{1,2})?$';
// Figures out if it's it's an valid percent
 '^[0-9]*(\.[0-9]+)?%$';

// which i turned into this statement....
 '(^[0-9]*(\.[0-9]+)?%$)|(^[0-9]*(\.[0-9]{1,2})?$)';

here is the output of my test script...
http://mightofnight.com/forum/reg_test.php

here is my script
http://mightofnight.com/forum/reg_test.phps

ok my problem is that after i did that i realzed that i needed a preg_match statement and that for some reason my regular expression doesn't work and i have no information on how a preg_match statement is different from a regex one


As always any ideas, links or help is greatly appreciated..
0
Comment
Question by:mightofnight
3 Comments
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 1000 total points
ID: 13904735
You need to add a forward slash ("/") to the beginning and end of your patterns and then call:
if (preg_match($test_ereg, $data)) {

Quote from:

http://us4.php.net/manual/en/ref.pcre.php

"The syntax for patterns used in these functions closely resembles Perl. The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters."

Related reference:

preg_match maunal entry: http://us4.php.net/manual/en/function.preg-match.php
pattern syntax: http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php

... note that with perl compatible regex you can optionally use \d instead of [0-9] which represents any decimal digit

Diablo84
0
 
LVL 7

Accepted Solution

by:
jdpipe earned 1000 total points
ID: 13908245
Hi

I just tested this with your $string array and it seemed to work:

print("<table>");
foreach($string as $s){
      
      print("<tr><td>".htmlspecialchars($s)."</td><td>\n");
      
      if(preg_match("#^\\$([0-9\\.]+)$#",$s,$matches)){
            print("amount: ".round($matches[1],2));
      }elseif(preg_match("#^([0-9]{0,2}(\\.[0-9]+)?)\\%?$#",$s,$matches)){
            print("percent: ".round($matches[1],4));
      }else{
            print("false");
      }
      print("</td></tr>\n");
}
print("</table>");
0
 
LVL 2

Author Comment

by:mightofnight
ID: 13913784
I got it to work :D

'/(^[0-9]*(\\.[0-9]+)?%$)|(^[0-9]*(\\.[0-9]{1,2})?$)/'

ok thanks for both of your comments..

jdpipe i am unable to directly use the statement you setup brecause this regex is going to be used in three different places... javascript form validation, regular form validation and and to determin the data type as needed.  Thouhg the example you provided did help me understand some of the syntex that i needed to change.  

Diable84 Thanks for the comment.  I wasn't really looking for a php.net link seeing as thats always the first place i look.  gotta love the quick searching php.net/*name of function or command here that you are looking for*.  But you did point me to a spot that i had overlooked.

THanks again guys

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question