c# - Parsing strings recursively -
c# - Parsing strings recursively -
i trying extract info out of string - fortran formatting string specific. string formatted like:
f8.3, i5, 3(5x, 2(a20,f10.3)), 'xxx'
with formatting fields delimited "," , formatting groups within brackets, number in front end of brackets indicating how many consecutive times formatting pattern repeated. so, string above expands to:
f8.3, i5, 5x, a20,f10.3, a20,f10.3, 5x, a20,f10.3, a20,f10.3, 5x, a20,f10.3, a20,f10.3, 'xxx'
i trying create in c# expand string conforms pattern. have started going lots of switch , if statements, wondering if not going wrong way?
i wondering if regex wizzard thinks regular expressions can in 1 neat-fell swoop? know nil regular expressions, if solve problem considering putting in time larn how utilize them... on other hand if regular expressions can't sort out i'd rather spend time looking @ method.
i suggest using recusive method illustration below( not tested ):
resultdata parse(string value, ref int32 index) { resultdata result = new resultdata(); index startindex = index; // used substrings while (index < value.length) { char current = value[index]; if (current == '(') { index++; result.add(parse(value, ref index)); startindex = index; continue; } if (current == ')') { // force lastly result index++; homecoming result; } // process other chars here } // can't find closing bracket throw new exception("string not valid"); }
you maybe need modify parts of code, method have used when writing simple compiler. although it's not completed, example.
c# regex
Comments
Post a Comment