Given this text
"Foo(Bar)"
I'd like to extract "Bar" using a regex.
Help!
Given this text
"Foo(Bar)"
I'd like to extract "Bar" using a regex.
Help!
/.*\((.*)\)/
The first .* will match anything before the parenthetical, the second .* (within a capture) will match anything inside the parenthetical and return it for you.
(I assumed that the quotes in your example text were not actually part of the string you're wanting to match.)
Just search for "Bar".
For example in Groovy:
if("Foo(Bar)" =~ /Bar/) {
println "match"
}
Will print 'match'
Should be enough:
\(.*\)
In Java the code would be:
String foo = "Foo(Bar)";
Pattern pattern = Pattern.compile("\(.*\)");
Matcher matcher = pattern.matcher(foo);
while (matcher.find()) {
int beginIndex=matcher.start();
int endIndex=matcher.end();
return foo.substring(beginIndex, endIndex);
You are omitting quite a bit of information: no platform or language used, no indications about the types of text you receive (i.e. is "Foo" static or can it change? Is "Bar" static? etc.).
Picking Ruby and assuming that "Foo" is stable, you can do this:
$ ruby -e 'p "Foo(Bar)"[/Foo\(([^)]*)\)/, 1]'
"Bar"
$