How can the regex pattern be modified to capture both the text before and inside parentheses, while also accounting for cases where there are no parentheses in the string?
To capture both the text before and inside parentheses, we can modify the regex pattern to include optional groups for the text before and inside parentheses. This can be achieved by using the `?` quantifier to make the groups optional. Additionally, we can use the `preg_match` function in PHP to extract the desired text based on the modified regex pattern.
$string = "Text (Inside Parentheses)";
$pattern = '/(.*?)(?:\((.*?)\))?/';
if (preg_match($pattern, $string, $matches)) {
$textBeforeParentheses = $matches[1];
$textInsideParentheses = $matches[2] ?? '';
echo "Text before parentheses: $textBeforeParentheses\n";
echo "Text inside parentheses: $textInsideParentheses\n";
}