java - regular expression to cut text -


i want cut text

unit=1111,space=(trk,0),disp=(mod,delete,delete),dsn=uuu.aaaaa.bbbbb 

result :

unit=1111 space=(trk,0) disp=(mod,delete,delete) dsn=uuu.aaaaa.bbbbb 

i tried myself m noob regular expression, used (\s+)=(\s+) cut not work correct.

someone me ?

here java code

import java.util.regex.matcher; import java.util.regex.pattern;  final string regex = "(\\s+)=(\\s+)"; final string string = "unit=1111,space=(trk,0),disp=(mod,delete,delete),dsn=uuu.aaaaa.bbbbb"  final pattern pattern = pattern.compile(regex); final matcher matcher = pattern.matcher(string);  while (matcher.find()) {     system.out.println("full match: " + matcher.group(0));     (int = 1; <= matcher.groupcount(); i++) {         system.out.println("group " + + ": " + matcher.group(i));     } } 

you can use negative lookahead regex splitting:

string[] arr = str.split(",(?![^()]*\\))"); 

this assuming ( , ) balanced , unescaped.

regex demo

regex breakup:

  • ,: match literal comma
  • (?![^()]*\\)): negative lookahead assert comma not inside (...)

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -