简体   繁体   中英

Split given string using java regex and not normal string.split method

I have a string like

ABC : blablabla,DEF : blablabla,GEH : blablabla etc...

I want to split above string to capture only the 3 letter words using a comma and finally save value to DB
Sometimes there might be null value completely or even single key:value pair without any comma's.

Does anyone has any idea..?

Here is how it's done with a Matcher , it's so short that I will give it away, but you should describe your approach first and why it is not working.

Pattern pattern = Pattern.compile("(?:[^a-zA-Z]+|^)([a-zA-Z]{3}) *:");

Matcher matcher = pattern.matcher(...);
while (matcher.find()) {
  System.out.println(matcher.group(1));
}

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