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 3 of 3
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Regular Expression - Finding numbers.

    PHP Code:
    preg_match('/\[Changeset:[0-9]+\]/''[Changeset:5048]'$matchesPREG_OFFSET_CAPTURE);

    print_r($matches); 

    What I'm trying to do is get the number 5048, but I can't see to get it.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You need parenthesis to capture the subpattern you want to match:
    PHP Code:
    preg_match('/\[Changeset:([0-9]+)\]/''[Changeset:5048]'$matchesPREG_OFFSET_CAPTURE);

    print_r($matches);

    //Array ( [0] => Array ( [0] => [Changeset:5048] [1] => 0 ) [1] => Array ( [0] => 5048 [1] => 11 ) ) 

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Thanks; that did the trick.

    Also, is there a way to add one to the integer found? Without the use of using a PHP function, I mean.

    Something like:

    return preg_replace("/Changesets:([0-9+1]+)-([0-9]+)/", "Changesets $1 to $2", "Changesets:5048-5500");
    Last edited by Apothem; 08-01-2008 at 05:33 AM.


  •  

    Posting Permissions

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