2

I am currently working at a project involving regex in php. I wanted to know why or how can I get this recursive regular expression to work in PHP:

/\(((?:(?:[^?+*{}()[\]\\|]+|\\.|\[(?:\^?\\.|\^[^\\]|[^\\^])(?:[^\]\\]+|\\.)*\]|\((?:\?[:=!]|\?<[=!]|\?>)?(?1)??\)|\(\?(?:R|[+-]?\d+)\))(?:(?:[?+*]|\{\d+(?:,\d*)?\})[?+]?)?|\|)*)\)/

It should match this text (for example):

{{"test":"([a-f0-9]{32})"},{"test2":"([a-z]{3})"}}

And the given results should be an array with:

  • [a-f0-9]{32}
  • [a-z]{3}

EDIT:

$from = '{{"test":"([a-f0-9]{32})"},{"test2":"([a-z]{3})"}}'; 
preg_match_all("/(((?:(?:[^?+*{}()[]\\|]+|\\.|[(?:\^?\\.|\^[^\]|[^\\^])(?:[^]\]+‌​|\\.)*]|((?:\?[:=!]|\?<[=!]|\?>)?(?1)??)|(\?(?:R|[+-]?\d+)))(?:(?:[?+*]|\{\d+‌​(?:,\d*)?\})[?+]?)?|\|)*))/", $from, $output_array, PREG_OFFSET_CAPTURE); 
var_dump($output_array);
4
  • 1
    can you show us, what you tried in php? Commented Nov 23, 2015 at 16:00
  • Can you provide test cases that require the use of recursions ? Commented Nov 23, 2015 at 16:01
  • Throw your own title into google and there is your answer. Commented Nov 23, 2015 at 16:01
  • 1
    Welcome to Stack Overflow! Please edit your question and don't add code/information in the comments. Commented Nov 23, 2015 at 16:03

1 Answer 1

3

Why this complex?

preg_match ('/\:"\((.*)\)"/', $search, $ matches);

It seems you just want the values between :"( and )"?

The matches are in $matches.

See http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

9 Comments

Because I wanted to match all regex in string "from", but your answer makes me think at an alternative. I wanted to use it for this: prntscr.com/961c6c
I want to use each regex from left and get each value and then use it in right textarea using $1, $2, etc...
@MariusEugenCirciu, seems like you have a valid json, then why do you need regexp? Decode json, change it how you need it, encode.
I have edited my post and it should work in this way. I added : in regex. Only if your regex in $from contains the sequence :"( or ") it wont work.
@radioheaded, because each json has id's (matching 32 chars from a-f0-9) and i want to replace entire json with json from right and then use regex from left into right side (as i said $1, $2, etc). I want to make it simplier but i don't know how...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.