Need some help with an regex

Posted on 2005-05-01
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
// Figures out if it's it's an valid percent

// which i turned into this statement....

here is the output of my test script...

here is my script

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..
Question by:mightofnight
    LVL 27

    Assisted Solution

    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:

    "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:
    pattern syntax:

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

    LVL 7

    Accepted Solution


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

    foreach($string as $s){
                print("amount: ".round($matches[1],2));
                print("percent: ".round($matches[1],4));
    LVL 2

    Author Comment

    I got it to work :D


    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 link seeing as thats always the first place i look.  gotta love the quick searching*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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now