Jump to content

regex not working


Destramic

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.