Jump to content

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

Edited by Destramic
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) Edited by requinix
Link to comment
https://forums.phpfreaks.com/topic/293340-regex-not-working/#findComment-1500646
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.