🔍Regex Recipes
Extract Content Inside Parentheses
Extract text inside parentheses with nested limitation notes.
Pattern
\(([^)]*)\)Explanation
Captures text within parentheses. Note: Does not handle nested parentheses reliably.
Examples
Simple
Input
Function(argument)
Output
Captures: argument
Multiple
Input
foo(bar) and baz(qux)
Output
Captures: bar, qux
Nested limitation
Input
outer(inner())
Output
Captures: inner( [incomplete]
Empty
Input
Empty()
Output
Captures: (empty)
Code Examples
JavaScript
const text = 'calculate(10) and format(result)';
const matches = [...text.matchAll(/\(([^)]*)\)/g)];
const contents = matches.map(m => m[1]);
// Result: ['10', 'result']
// For nested parentheses, use a parser or recursive regex
function extractWithNesting(text) {
const stack = [];
let current = '';
let depth = 0;
for (const char of text) {
if (char === '(') {
if (depth > 0) current += char;
depth++;
} else if (char === ')') {
depth--;
if (depth === 0) {
stack.push(current);
current = '';
} else {
current += char;
}
} else if (depth > 0) {
current += char;
}
}
return stack;
}Try it Now
💡 Tips
- Use [^)] to match anything except closing paren
- For nested content, consider recursive regex (PCRE) or parser
- Balance checking requires additional logic
- Test with your actual use case data
⚠️ Common Pitfalls
- Cannot reliably handle nested parentheses
- Breaks with unbalanced parentheses
- For complex nesting, use a proper parser
- May need different approach for programming languages