Reputation: 814
I have the following string {[ab]} {[c] } { [d] } {[f]} { = [abc] qwqw = =qwwq= =wq }
, I'm trying to extract a group by using a specific string which is inside one of the parentheses, e.g:
ab -> {[ab]}
d -> { [d] }
abc -> { = [abc] qwqw = =qwwq= =wq }
Once I get the result I want to replace it with something else.
I've attempted several different regex patterns:
\{(.*?\[(?:abc)\].*?)\}
([^}][{].*?\[(?:abc)\].*?[}])
({+?(?<=)\[(?:abc)\].*?[}])
^(?:.*)({.*?\[(?:abc)\].*?[}])
But none of them work properly. They either match too much of the string or too little of the string.
Upvotes: 0
Views: 183
Reputation: 163277
This part (?:abc)
will match a string abc
and does not need the non capturing group. That will not match a single a or ab etc.
One option could be use a capturing group and a character class repeating 1 or more times any of the listed [abcd]+
which might also be aaa if that is ok.
\{[^{}]*\[([abcd]+)\][^{}]*\}
In parts
\{
Match {
[^{}]*
Match 0+ times not {
or }
\[
Match [
([abcd]+)
Capture group 1, match 1+ times any of the listed\]
Match ]
[^{}]*
Match 0+ times not {
or }
\}
Match }
See a Regex demo
If you only want to match abc ab c or d you could also use an alternation matching ab with an optional c, or c or d
\{[^{}]*\[(abc?|c|d)\][^{}]*\}
Upvotes: 1