简体   繁体   中英

Regular expression to not to split if it's inside a particular pattern

I have a custom string which is a streamed version of multiple item. Item can be Integer, String and List of String where each item represented by I,S,L respectively.

For eg: String d = "I:123;S:345;L:{S:45;S:67;S:789};I:23";

I am trying to write a single regular expression which could split this string and should produce 4 items

1. I:123,  2. S:345, 3.L:{s:45;s:67;s:789}; ,4:I:23

If I just split based on ;then it will split items List of also that should not happen.Then I tried to write some complex regular expression but nothing worked.

Could some one please give some pointer?

You should use the lazy quantifiers:

(.+?);(.+?);(.*)

Links:

You may use this lookahead based regex for splitting:

String[] arr = str.split(";(?![^{}]*})");

RegEx Demo

Details:

  • ; : Match literal ;
  • (?![^{}]*}) : Negative lookahead to make sure we don'r have a } after non { and } characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM