Trying to create a regex that match any character inside a parentheis.
My regex pattern is this
preg_match('/\((.*?)\)/', $listanswer, $answer);
All string inside the parenthesis is the matching pattern. But the problem is, when I try to match eg,. (this word), (sample data)
it only returns null. When if no space added, it is matched. Any idea on this?
Already tested it here. http://regex101.com
It worked just fine. Did i miss something>?
Try this
\(([^)]+)\)
\(
: match an opening parentheses(
: begin capturing group[^)]+
: match one or more non )
characters)
: end capturing group\)
: match closing parenthesesSince you don't have (nested (parentheses))
in your input:
To match (the parentheses too)
use \([^)]*\)
(demo)
To just match (what's inside?
) use (?<=\()[^)]*(?=\))
(demo)
(?<=\()
lookbehind asserts that what precedes is an opening parenthesis. Then we match any chars that are not a closing parens. Then the (?=\))
lookahead asserts that what follows is an closing parenthesis.