Debiprasad Behera
Debiprasad Behera

Reputation: 63

Regex for not including the contain of a closed curly braces in Java

Please help me to solve this in java.

input string = <V2>UTM_Source:google|UTM_Medium:cpc|UTM_Campaign:{Core|IN|Desktop|BMM|Top Cities|TS}|
UTM_Content:{Compare Car Insurance}|UTM_Term:

I want to split with "|" but not the inside contain of curly braces So the output will be:

<V2>UTM_Source:google
UTM_Medium:cpc
UTM_Campaign:{Core|IN|Desktop|BMM|Top Cities|TS}
UTM_Content:{Compare Car Insurance}
UTM_Term:

Thanks in advance.

Upvotes: 0

Views: 190

Answers (1)

Scott Weaver
Scott Weaver

Reputation: 7361

So basically, you want to match the entire {...} sequences all at the same time, or in other words, treat them as a single character within your regular expression: \{.*?\} Using this fragment as the first choice in an alternation with a single "regular non pipe" character, and then letting that whole thing repeat, we avoid spurious matches inside the curly brackets:

((?:\{.*?\}|[^|])+)\|

or as Sven points out, you don't even need that last | or the capturing group:

(?:\{.*?\}|[^|])+

demo

Upvotes: 1

Related Questions