PHP Regex help

Discussion in 'PHP' started by Dman91, Sep 11, 2008.

  1. #1
    <?php
    $test = '-1 -134123 -1 -1';
    preg_match_all("/\b-1\b/" , $test , $match);
    print_r($match);
    ?>
    PHP:
    i am trying to match only -1 and not -1224 (that is why i used boundaries) but code does not work (it works for positive numbers)
     
    Dman91, Sep 11, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    
    preg_match_all("|(-[0-1]{1})|" , $test , $match);
    
    PHP:
    Should match negative any 1 number. I don't think so, but you might need to escape the negative sign.

    Peace,
     
    Barti1987, Sep 11, 2008 IP
  3. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It matches but i tried adding boundaries and it doesnt work.
     
    Dman91, Sep 11, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Try -1[^0-9]

    That means -1 and it cant be followed by a number. I don't know about boundary, but if you using doublequote, you need to escape it \\b
     
    Kaizoku, Sep 11, 2008 IP
  5. beacon

    beacon Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This work
    $test = '-1 -134123 -1 -1';
    preg_match_all("~-1( |$)~" , $test , $match);
    print_r($match);
    Code (markup):
     
    beacon, Sep 11, 2008 IP
  6. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    using \\b doesnt work and -1[^0-9] doesnt solve my purpose .. Thanks for helping though :D
    Edit this works great beacon Thanks! what if i want it to match -1 or 1 ?
     
    Dman91, Sep 11, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Put a ? after the -

    Jay
     
    jayshah, Sep 11, 2008 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think the problem lies with you using double quotes and the \. Use
    '/\b\-1\b/'
     
    JAY6390, Sep 11, 2008 IP
  9. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @jayshah - doesnt work ...
    @JAY6390 - this doesnt work too...
     
    Dman91, Sep 11, 2008 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Use
    <?php
    $test = '-1 -134123 -1 -1';
    preg_match_all('/\-1\b/' , $test , $match);
    print_r($match);
    PHP:
    it returns
    Array
    (
        [0] => Array
            (
                [0] => -1
                [1] => -1
                [2] => -1
            )
    
    )
    Code (markup):
     
    JAY6390, Sep 12, 2008 IP