PHP - Regex for matching string containing pattern but without pattern itself

The sample file:

dept1: user1,user2,user3
dept2: user4,user5,user6
dept3: user7,user8,user9

I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex?

Try:

awk '/^dept2/{print $2}' file

I need to use it in php script. Calling awk from there sounds not good. Moreover i can split string into array and shift it, but i'd like to know regex-based solution just to extend my regex knowledge.

What function are you using (if any), and are you passing it the whole file or 1 line at a time?

I'm using preg_match_all('/(dept1.*|dept2.*|dept3.*)/i', $curl_out, $userarray1, REG_SET_ORDER);
$curl_out - is input multiline variable (curl execution)
$userarray1 - is output multiline array

whether like this do you expect output to be ?

akshay@Aix:~$ php -r 'echo preg_replace("/dept.*: /","", "dept1: user1,user2,user3")."\n"; '
user1,user2,user3
akshay@Aix:~$ php -r '$var = spliti ("dept.*:", "dept1: user1,user2,user3"); print_r($var);'
Array
(
    [0] => 
    [1] =>  user1,user2,user3
)

Note : This function has been DEPRECATED as of PHP 5.3.0

I don't have PHP handy at the moment, but try:

preg_match_all ('/^(dept1|dept2|dept3)(.*)$/im', $curl_out, $userarray1, PREG_PATTERN_ORDER);

This gave me no uotput at all :frowning:

It gives the same output your preg_match_all() call will :P. The matches will be in the array.

~/tmp$ cat test.php
<?php
   $curl_out="dept1: user1,user2,user3
dept2: user4,user5,user6
dept3: user7,user8,user9";

   preg_match_all ('/^(dept1|dept2|dept3)(.*)$/im', $curl_out, $userarray1, PREG_PATTERN_ORDER);

   print_r ($userarray1);
?>
~/tmp$ php test.php
Array
(
    [0] => Array
        (
            [0] => dept1: user1,user2,user3
            [1] => dept2: user4,user5,user6
            [2] => dept3: user7,user8,user9
        )

    [1] => Array
        (
            [0] => dept1
            [1] => dept2
            [2] => dept3
        )

    [2] => Array
        (
            [0] => : user1,user2,user3
            [1] => : user4,user5,user6
            [2] => : user7,user8,user9
        )

)