How to generate a regex to capture everything but curly brace contents

63 Views Asked by At

I'm trying to match strings of any size NOT surrounded with { and } as in foo{bar} the regex should match foo but not {bar}.

The regexes I originally came up with were ^([^${].*[}$]) and ^(?=[{]).+(?<=[}]) but they don't seem to do what I expected them to do.

1

There are 1 best solutions below

0
On BEST ANSWER

If you want to fetch all the characters that is not within {} then you can attempt an split operation using regex.

Split the string by this regex by using your preferred language:

{.*?}

The returned array should consist the segments that was found outside each {}

The following java example returns an array (arr):

String abc="19{22}33{44}55{66}7";
String[] arr=abc.split("\\{.*?\\}");

which contains:

["19","33","55","7"]