What reg expression can be used to get text between ( and ).
| SitePoint Sponsor |
What reg expression can be used to get text between ( and ).


Parentheses have "special meaning" in regex (they signify a "capture") so you need to escape them with a backslash so they are "literal" parentheses.
Then depending on what you want to match and capture between them, you can either use a character class or the text strings in your expression.
I pretty much want to delete text between the Parentheses.
A example of the reg expression would be nice lol, I still cant get mine to work.


So you want to keep the parentheses, and any non-text characters, removing only, and all, the text characters, if any are there?

PHP Code:$s = "This is some (stupid) text (poop).";
echo preg_replace("/\((.)+?\)/","(newtext)",$s);
The above regular expression might not be what you're after since it will have problems with nested sets of parentheses.
The original post isn't very precise in saying what the expected inputs and outputs should be. Do you just want to 'get' the text between parentheses, or delete that text. Define 'text', is that anything at all?
Anyway, here's my two pennies.
Of course, that is a bit of a mouthful! An example of its use would be:Code:/ \( # Opening parenthesis (?> # Start once-only sub-pattern [^()]+ # One or more non-parenthesis characters | # Or (?R) # Recursive match of the entire pattern (ie, nested parentheses) )* # End once-only sub-pattern, match it zero or more times \) # Closing parenthesis /x
If you want to keep the wrapping parentheses, just include them in the replacement.PHP Code:$text = 'This is (not) a (quick (useful)) test.';
echo preg_replace('/\((?>[^()]+|(?R))*\)/', '', $text); // This is a test.
Bookmarks