I know the title is not the clearest but I did not know how to word it better.
What I want to do is to capture in a single group whatever is input, unless there's a particular structure around it, in which case just discard the structure and keep the rest.
I'll give some examples. Imagine the structure was cap(.*), I'd like to have the following:
Input:
"cap(text)"
"cap(text"
"text)"
Output:
"text"
"cap(text"
"text)"
I'm stuck at something like
(?:cap\()?(.*)
But then it does not consider simultaneously both the "cap(" and the ")", and therefor examples 1 and 2 do not work.
Also
cap\((.*)\)|(.*)
doesn't work, as it separates the content in two groups, depending on the case.