phpregexpreg-matchtext-extraction

Get numeric value between unique string and underscore


Can someone tell me how to get "123" out of this string with regex:

.../groups/123_abc/...

I am sure that

/groups/

is unique in that string.


Solution

  • (?<=groups\/)\d+
    

    Try this.See demo.

    https://regex101.com/r/sJ9gM7/57

    $re = "/(?<=groups\\/)\\d+/im";
    $str = ".../groups/123_abc/...";
    
    preg_match_all($re, $str, $matches);