Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    Regex Number followed by symbol issue

    Why is the 3rd term not -6_?

    PHP Code:
    $lefteq="-x+2+10x-9-6_";
    $lct preg_match_all('/(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/'$lefteq$matchPREG_PATTERN_ORDER);
    for (
    $x=0;$x<$lct;$x++){
    $curvalue=$match[0][$x];
    echo 
    "curvalue = " $curvalue "<br />";
    $curvalue=substr($curvalue,0,strlen($curvalue)-1);
    array_push($lconst,$curvalue);

    which produces

    PHP Code:
    curvalue 2+
    curvalue = -9-
    curvalue 6_ 

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $lct = preg_match_all('/(\-)?(\d+?)(\-|\+|\=|\>|\<|\_)/', $lefteq, $match, PREG_PATTERN_ORDER);
    or:

    Code:
    $lct = preg_match_all('/(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/u', $lefteq, $match, PREG_PATTERN_ORDER);
    or:

    Code:
    $lct = preg_match_all('/^(\-)?(\d+)(\-|\+|\=|\>|\<|\_)/', $lefteq, $match, PREG_PATTERN_ORDER);
    should do as you expect.

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    2 of them did the same as what I had, and the one with the line starter ^ showed nothing.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    What is it supposed to match? All or part of the string? If partial, which part? Btw, do a search on Google or such for 'PCRE cheat sheet'. It'll help with the specifics of regex.

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    It is supposed to match any number, positive or negative, followed by an operator sign or an underscore.

    The only trouble I have in unit testing is that a negative number followed by a negative number comes up as negative positive. Everything else works like a charm.

    I'll check the cheatsheet out.

  • #6
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Tried this instead, I removed the underscore from the end of $lefteq:
    I'm looking for any number not followed by a letter. This gives the last negative number, but also gives the 1 in the number 10.

    PHP Code:
    $lct preg_match_all('/(\-?)(\d+)(?![a-z])/'$lefteq$matchPREG_PATTERN_ORDER); 

  • #7
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Fixed. I needed boundaries on the number. This solves my problem:

    PHP Code:
    $lct preg_match_all('/(\-?)\b(\d+)\b(?![a-z])/'$lefteq$matchPREG_PATTERN_ORDER); 

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by mathceleb View Post
    I'm looking for any number not followed by a letter.
    I'm a bit rusty, but try this:

    Code:
    preg_match_all('/(\d+)(?![a-z])/', $lefteq, $match, PREG_PATTERN_ORDER);
    Try looking in $match[1] instead of $match[0] too.


    Edit: You shouldn't need to use the boundary marker.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •