0

Detecting a parenthesis pattern in a string

This is a line (an example between parenthesis).

or

This is a line(an example between parenthesis).

I need to separate both strings in:

$text = 'This is a line.';

$eg = 'an example between parenthesis';

I have this code so far:

$text = 'This is a line (an example between parenthesis)';
preg_match('/\((.*?)\)/', $text, $match);
print $match[1];

But it only brings the text inside the parenthesis. I also need the text outside the parenthesis.

oberfreak
  • 1,799
  • 13
  • 20
Andres SK
  • 10,779
  • 25
  • 90
  • 152
  • See also http://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world - or - http://stackoverflow.com/questions/32282/regex-testing-tools – mario Nov 28 '11 at 22:06
  • May I ask what this is for? Depending on use cases it may needs something more than Regular Expressions. (Crazy idea, right?) – Levi Morrison Nov 28 '11 at 22:07
  • @LeviMorrison im using a spanish dictionary api. They send both the definitions and examples in the same string. Examples come between parenthesis and I need to place them on different – Andres SK Nov 28 '11 at 22:10

4 Answers4

3
$text = 'This is a line (an example between parenthesis)';
preg_match('/(.*)\((.*?)\)(.*)/', $text, $match);
echo "in parenthesis: " . $match[2] . "\n";
echo "before and after: " . $match[1] . $match[3] . "\n";

UPDATE after clarification of question .. now with many parenthesis:

$text = "This is a text (is it?) that contains multiple (example) stuff or (pointless) comments in parenthesis.";
$remainder = preg_replace_callback(
        '/ {0,1}\((.*)\)/U',
        create_function(
            '$match',
            'global $parenthesis; $parenthesis[] = $match[1];'
        ), $text);
echo "remainder text: " . $remainder . "\n";
echo "parenthesis content: " . print_r($parenthesis,1) . "\n";

results in:

remainder text: This is a text that contains multiple stuff or comments in parenthesis.
parenthesis content: Array
(
    [0] => is it?
    [1] => example
    [2] => pointless
)
Kaii
  • 20,122
  • 3
  • 38
  • 60
  • 1
    This will also (seriously) fail on things that have lots of (sometimes-pointless) comments. – Levi Morrison Nov 28 '11 at 22:07
  • @LeviMorrison your comment is based on the assumtion that the OP wants to handle (possibly-multi-line) text with (possibly-too-much) (possibly-pointless) comments in parenthesis. If so, this would indeed require a different solution. – Kaii Nov 28 '11 at 22:10
  • works very good! im also trying to make it work for cases like this: "this is a line (some content) and it looks very nice (some extra)" ~ would it be possible to match both parentheses content into the same variable as well? – Andres SK Nov 28 '11 at 22:11
  • clap. clap. clap. this actually works perfectly well. thanks! – Andres SK Nov 28 '11 at 22:41
1

You could use preg_split for this specific task. Ignore last array value if ther is no text after closing parenthesis.

$text = 'This is a line (an example between parenthesis)';
$match = preg_split('/\s*[()]/', $text);
vik
  • 762
  • 1
  • 7
  • 18
  • the problem with this regex is that you can't know which splits are original texts and which are original between parenthesis. – Andres SK Nov 28 '11 at 22:39
  • Right, If you are not sure you have parenthesis in $text, then I rather would use two different regular expression as all in one regular expressions. With this one, always the second array value is in text in parenthesis and first or last is empty and the other has the text outside of parenthesis. – vik Nov 28 '11 at 23:04
0

All the text should be in $match[0]. If you want to get the text before and the text after, simply rewrite your regex like so:

/(.*?)\((.*)\)(.*?)/

Then. the text before will be in $match[1] and $match[3].

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72
0

I think you can try with this regex ([^\(].[^\(]*)(\(\b[^\)]*(.*?)\)):

<?php
$text = 'This is a line (an example between parenthesis)';

preg_match_all('/([^\(].[^\(]*)(\(\b[^\)]*(.*?)\))/', $text, $match);

echo '<pre>';
print_r($match);
echo '<pre>';

$text = 'This is a line(an example between parenthesis)
This is a line (an example between parenthesis)
This is a line (an example between parenthesis)
This is a line (an example between parenthesis) This is a line (an example between parenthesis) This is a line (an example between parenthesis)';

preg_match_all('/([^\(].[^\(]*)(\(\b[^\)]*(.*?)\))/', $text, $match);

echo '<pre>';
print_r($match);
echo '<pre>';
?>

http://codepad.viper-7.com/hSCf2P

Galled
  • 4,146
  • 2
  • 28
  • 41
  • there might be the case where the string is: "This is a line (something) and some more." ~ im trying to figure out how your regex can be modified to also return the text after the parenthesis closure (if any). – Andres SK Nov 28 '11 at 22:18
  • @andufo Mmm I need more information, because this case occurs in the second example in the fourth line. Which could be the separator between each text? Since for this regular expression the end of a string is the closed parentheses. – Galled Nov 28 '11 at 22:21