split - Regex for splitting txt db file -
i'm writing txt database file sql converter, , need split items in rows. problem among items there might scripts can hold multiple commas (which separators in db structure). news, scripts nested {}-s so, makes job similar parsing csv file. problem scripts can hold more scripts nested {}-s, , stops formula working..
structure of txt db:
501,red_potion,red potion,0,50,,70,,,,,0xffffffff,7,2,,,,,,{ itemheal rand(45,65),0; },{},{} 502,orange_potion,orange potion,0,200,,100,,,,,0xffffffff,7,2,,,,,,{ itemheal rand(105,145),0; },{},{} 503,yellow_potion,yellow potion,0,550,,130,,,,,0xffffffff,7,2,,,,,,{ itemheal rand(175,235),0; },{},{} 504,white_potion,white potion,0,1200,,150,,,,,0xffffffff,7,2,,,,,,{ itemheal rand(325,405),0; },{},{} the regex use match delimeters splitting:
,(?![^{}]*\}) this works fine until counters more complicated nested script item, like:
1492,velum_glaive,vellum glaive,4,20,,4500,250,,3,0,0x00004082,7,2,34,4,95,1,5,{ bonus2 baddrace,rc_demihuman,80; if(getrefine()>=6) { bonus2 bskillatk,"lk_spiralpierce",100; bonus2 bskillatk,"kn_spearboomerang",50; } if(getrefine()>=9) { autobonus2 "{ bonus bshortweapondamagereturn,20; bonus bmagicdamagereturn,20; }",100,2000,bf_weapon|bf_magic,"{ specialeffect2 ef_reflectshield; }"; } },{},{} so how make match db structure delimeters , leave commas in script out?
thanks in advance! :)
this not job regex. pointed out in comment, nested structures beyond regex can do. pcre has recursion construct (?r) , .net has balanced groups, solution unreadable , unmaintainable.
added that, don't have take {} account, strings , comments in script well. better off, parsing thing manually. here quick , dirty example how done using php (ignoring strings , comments!):
$level = 0; $values = array(); $start = 0; for($i = 0; $i < strlen($str); $i++) { switch($str[$i]) { case ",": if(!$level) { $values[] = substr($str, $start, $i-$start); $start = $i+1; } break; case "{": $level++; break; case "}": $level--; if($level < 0) trigger_error("unexpected }"); break; } } if($level > 0) trigger_error("missing }"); $values[] = substr($str, $start); you can see already, end simpler parser script.
Comments
Post a Comment