1

I need a hand on my preg_replace pattern:

I want to replace the texts between brackets [] but also inside brackets and not only until the first one [.[.].....] and the same pattern but to replace only numbers inside those brackets.

any idea?

2
  • can you give an example? (Source and expected Result) but from what i guess your Case doesn't sound "regular" and by definition may not be matchable by regular expressions Commented Jan 28, 2011 at 11:20
  • Can you try to explain what you want a little more detailed, perhaps posting some sample input data along with expected result? Commented Jan 28, 2011 at 11:21

1 Answer 1

4

Try this regex:

$re = '#\[(?:.*?(?0))*.*?\]#'

This will match a [...] pair, which may itself contain one or more [...] pairs, with any characters between them. This is done by using recursion in the pattern (the (?0) calls the pattern again).

preg_match($re, '[.[.]....]', $m);
print_r($m);

// Output:
// Array
// (
//    [0] => [.[.]....]
// )
Sign up to request clarification or add additional context in comments.

Comments

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.