Regular expression

Discussion in 'PHP' started by benett, Jul 27, 2007.

  1. #1
    How can I write this POSIX regexp like Perl regexp?
    <b>\$([0-9]+\.[0-9]{2})-{0,1}\${0,1}([0-9]+\.[0-9]{2}){0,1}</b>
     
    benett, Jul 27, 2007 IP
  2. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well it would help for you to tell us what you're testing for.

    Perl regexp could probably work the same way except changing:
    /^ add that at beginning
    $/ and that at the end.
     
    Tarkan, Jul 27, 2007 IP
  3. benett

    benett Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am testing for two kinds of string:
    <b>$1.45-$14.67</b>
    <b>$1.45</b>
    first digit is long at least 1 character and the second is exactly 2 chars
     
    benett, Jul 27, 2007 IP
  4. Jezek

    Jezek Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Look at this site: regexlib.com. I believe they already have some code that does this.
     
    Jezek, Jul 27, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I don't think the start/end markers are needed, he probably wants to match this code segment in a HTML page.

    This should help (I also simplified some parts, e.g. changing "{0,1}" to "?"):
    
    my $html = "your HTML";
    my $price1 = 0.0;
    my $price2 = 0.0;
    
    if ($html =~ m/\<b>\$([0-9]+\.[0-9]{2})(-\$([0-9]+\.[0-9]{2}))?\</b>/) {
        $price1 = $1;
        $price2 = $3; # need a defined() check?
    }
    Code (markup):
     
    krt, Jul 27, 2007 IP