Destramic Posted December 25, 2014 Share Posted December 25, 2014 hey guys im trying to match a certain string with possible matchs like: :action(activation-key|activation_key2|activation_key3) ...etc :action(activation-key|activation_key2) :action(activation-key) (activation-key) ...etc unfortunatley im not getting the results im after and am now scratching my head. here is my code: $text = ":action(activation-key|activation_key2|activation_key3)"; if (preg_match_all('/<parameter>(.*?)\((.*?)|(.*?)\||\|(.*?)\)$/', $text, $match)) { print_r($match); } my result: Array([0] => Array([0] => :action(activation-key|[1] => activation_key2|)[1] => Array([0] =>[1] =>)[2] => Array([0] =>[1] =>)[3] => Array([0] => :action(activation-key[1] => activation_key2)[4] => Array([0] =>[1] =>)) a result like this is what im after if anyone could help please: Array([0] => Array(['parameter'] => :action[0] => activation_key1 [1] => activation_key2 [2] => activation_key3)) thank you guys Link to comment https://forums.phpfreaks.com/topic/293340-regex-not-working/ Share on other sites More sharing options...
requinix Posted December 26, 2014 Share Posted December 26, 2014 First the explanation. '/(.*?)\((.*?)|(.*?)\||\|(.*?)\)$/'The has to be in a (?P) to have meaning, or else it's literal. And the |s have a special meaning - alternation, where A|B means "match either A or B" - which is how your expression actually matches anything in the first place. Also only use preg_match_all() unless you want multiple matches in the source string - multiple parameter/argument sets; use preg_match() for just one. (I don't know, you might need multiple matches.) A single regex can't get exactly the output you want. Use one to get the parameter and the "arguments" to it, then explode the argument list (on "|") for the individual arguments. Then you can combine everything into one array. '/(?P:\w+)\((.*?)\)/'(adjust the \w+ as you see fit) Link to comment https://forums.phpfreaks.com/topic/293340-regex-not-working/#findComment-1500646 Share on other sites More sharing options...
Destramic Posted December 28, 2014 Author Share Posted December 28, 2014 works great thank you requinix Link to comment https://forums.phpfreaks.com/topic/293340-regex-not-working/#findComment-1500868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.