php - capturing group under capturing group? -
is possible capturing group under capturing group can have array that
regex = (asd1).(lol1),(asd2).(asd2)
string = asd1.lol1,asd2.lol2
return_array[0]=>group[0]='asd1';return_array[0]=>group[1]='lol1';return_array[1]=>group[0]='asd2';return_array[1]=>group[1]='lol2';
while using regular expressions can want, use strtok() iterate through seems comma separated sets:
$results = array(); $str = 'asd1.lol1,asd2.lol2'; $token = strtok($str, ','); while ($token !== false) { $results[] = explode('.', $token, 2); $token = strtok(','); } output:
array ( [0] => array ( [0] => asd1 [1] => lol1 ) [1] => array ( [0] => asd2 [1] => lol2 ) ) with regular expressions pattern needs include 2 terms surrounding period, i.e.:
$pattern = '/(?<=^|,)(\w+)\.(\w+)/'; preg_match_all($pattern, $str, $result, preg_set_order); the (?<=^|,) look-behind assertion; makes sure match comes after if preceded either start of search string or comma, doesn't "consume" anything.
output:
array ( [0] => array ( [0] => asd1.lol1 [1] => asd1 [2] => lol1 ) [1] => array ( [0] => asd2.lol2 [1] => asd2 [2] => lol2 ) )
Comments
Post a Comment