timukh
timukh

Reputation: 67

How to get the text between any number of parenthesis?

Suppose I have a document where I want to capture the strings that have parenthesis before or after.

Example: This [is] a {{test}} sentence. The (((end))).

So basically I want to get the words is, test and end.

Thanks in advance.

Upvotes: 0

Views: 98

Answers (4)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Try this Regex:

(?<=\(|\[|{)[^()\[\]{}]+

>>>Demo<<<

OR this one:

(?<=\(|{|\[)(?!\(|{|\[)[^)\]}]+

>>>Demo<<<

Explantion(for the 1st regex):

  • (?<=\(|\[|{) - Positive lookbehind - looks for a zero-length match just preceeded by a { or [ or a (
  • [^()\[\]{}]+ - one or more occurences of any character which is not amoong the following characters: [, (, {, }, ), ]

Explanation(for 2nd Regex):

  • (?<=\(|\[|{) - Positive lookbehind - looks for a zero-length match just preceeded by a { or [ or a (

  • (?!\(|{|\[) - Negative lookahead - In the previous step, it found the position which is just preceded by an opening bracket. This piece of regex verifies that it is not followed by another opening bracket. Hence, matching the position just after the innermost opening bracket - (, { or [.

  • [^)\]}]+ - One or more occurrences of characters which are not among these closing brackets - ], }, )

Upvotes: 1

Marc Lambrichs
Marc Lambrichs

Reputation: 2892

Your regex could be:

[\[{(]((?(?<=\[)[^\[\]]+|(?(?<={)[^{}]+|[^()]+)))

Explanation: the if-then-else construction is needed to make sure that an opening '{' is matched against a closing '}', etc.

[\[{(]                                   # Read [, { or (
((?(?<=\[)                               # Lookbehind: IF preceding char is [
 [^\[\]]+                                # THEN read all chars unequal to [ and ]
 |                                       # ELSE
 (?(?<={)                                # IF preceding char is {
 [^{}]+                                  # THEN read all chars unequal to { and }
 |                                       # ELSE
 [^()]+)))                               # read all chars unequal to ( and )

See regex101.com

Upvotes: 1

Oops
Oops

Reputation: 1413

The below code works for me.

<?php

$in = "This [is] a {{test}} sentence. The (((end))).";
preg_match_all('/(?<=\(|\[|{)[^()\[\]{}]+/', $in, $out);
echo $out[0][0]."<br>".$out[0][1]."<br>".$out[0][2];
?>

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

According to your condition "strings that have parenthesis before or after" - any word could be proceeded with OR only followed by some type of parentheses:

$text = 'This [is] a {{test}} sentence. The (((end))). Some word))';
preg_match_all('/(?:\[+|\{+|\(+)(\w+)|(\w+)(?:\]+|\}+|\)+)/', $text, $m);
$result = array_filter(array_merge($m[1],$m[2]));

print_r($result);

The output:

Array
(
    [0] => is
    [1] => test
    [2] => end
    [7] => word
)

Upvotes: 1

Related Questions