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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    Regex digit plus sign digit

    The variable "add_count" is always 1. Here is what I tried:

    PHP Code:
    $n="6+0+1";
    $add_count preg_match_all('/-?\d+(\+)\d+/'$n$match2); 
    Also tried this:

    PHP Code:
    $n="6+0+1";
    $add_count preg_match_all('/\b-?\d+(\+)\d+$/'$n$match2); 
    It still evaluates to 1 and not 2.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The first expression matches to 6+0 and then decides that +1 doesn't match.

    The second one matches 0+1 and then doesn't have any further content to check.

    So on both cases there is only one number+number that can be matched (given that the search for another match starts from the end of the previously matched text).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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