Home > Software design >  Capture everything but a particular structure using a single group
Capture everything but a particular structure using a single group

Time:01-06

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.

CodePudding user response:

You can replace

cap\((.*?)\)

with

$1

See the regex demo.

Here, cap\( matches cap(, the (.*?) capturing group matches any zero or more chars other than line break chars, as few as possible, and the \) part matches a ). The $1 is a placeholder for Group 1 value.

If cap must be matched as a whole word, prepend it with a word boundary, \b:

\bcap\((.*?)\)
  •  Tags:  
  • Related