java - RegEx for Complex String -


i new using regex , trying use java engine. example string trying parse following:

name:"sfatg";affil:100;aup:1;bu:false name:"sf tac 1";affil:29.3478;aup:19;bu:false name:"sf tac 2";affil:22.2222;aup:14;bu:false name:"sf tac 3";affil:44.4444;aup:0;bu:false name:"sf disp 4";affil:82.4742;aup:0;bu:false  

what hope regex achieve extract values appear after : , before ;. in addition, not want include quotes within entries name. however, (in particular case) would keep space appears in entry bu. not, however, want have name field appear data entry of bu though. i'd want false, not false name field.

my ultimate goal using regex create array of groups/data values array contain following:

[0]: sfatg [1]: 100 [2]: 1 [3]: false  [4]: sf tac 1 ...etc. 

i thinking creating groups each value because able create array combining pattern , matcher classes, such that:

string regex = "some fancy regex works"; pattern p = pattern.compile(regex); matcher m = p.matcher("some really long string follows outlined format"); // i'd want use object array since data values vary type // can create 4 different arrays (one name, affil, etc.), // advice on approach take? object[] datavalues = new object[m.groupcount()]; 

the regex i've far been able come follows:

name:"(\w+)";affil:(\d+);aup:(\d+);bu:(\w+\s) 

however, seems work on first 4 data values , none beyond that.

would able assist me on creating regex data working with? assistance on appreciated! i'm open ideas on how else approach this, such using different data type storing data afterwards (other creating object array). key somehow obtain data values string i've mentioned , storing them processing occur later on.

additional question i'd imagine there may external libraries may have been better fit perform task. aware of library work this?

while regex less general purpose @jan's answer, restrict matches fields in data, provide syntax checking:

name:"([^"]+)";affil:([\d.]+);aup:(\d+);bu:(true|false) ? 

regarding approach extracting values, i'd create thin wrapper object provide type safety:

public class rowparser {     private static final pattern row_pattern = pattern.compile("name:\"([^\"]+)\";affil:([\\d.]+);aup:(\\d+);bu:(true|false) ?");      public static void main(string[] args) {         string data = "name:\"sfatg\";affil:100;aup:1;bu:false name:\"sf tac 1\";affil:29.3478;aup:19;bu:false name:\"sf tac 2\";affil:22.2222;aup:14;bu:false name:\"sf tac 3\";affil:44.4444;aup:0;bu:false name:\"sf disp 4\";affil:82.4742;aup:0;bu:true \n";         system.out.println(parserows(data));     }      public static list<row> parserows(string data) {         matcher matcher = row_pattern.matcher(data);         list<row> rows = new arraylist<>();         while (matcher.find()) {             rows.add(new row(matcher));         }         return rows;     }      // wrapper object individual data rows     public static class row {         private string name;         private double affil;         private int aup;         private boolean bu;          row(matcher matcher) {             this.name = matcher.group(1);             this.affil = double.parsedouble(matcher.group(2));             this.aup = integer.parseint(matcher.group(3));             this.bu = boolean.parseboolean(matcher.group(4));         }          public string getname() {             return name;         }          public double getaffil() {             return affil;         }          public int getaup() {             return aup;         }          public boolean isbu() {             return bu;         }          @override         public string tostring() {             return "name:\"" + name + '"' + ";affil:" + affil + ";aup:" + aup + ";bu:" + string.valueof(bu).touppercase();         }     } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -