c# - Complex regex or string parse -


we trying use urls complex querying , filtering.
managed of simpler parst working using expression trees , mix of regex , string manipulation looked @ more complex string example

 var filterstring="(|(^(categoryid:eq:1,2,3,4)(categoryname:eq:condiments))(description:lk:”*and*”))"; 

i'd able parse out in parts allow recursive.. i'd out put looking like:

   item[0] (^(categoryid:eq:1,2,3,4)(categoryname:eq:condiments)    item[1] description:lk:”*and*” 

from there strip down item[0] part get

categoryid:eq:1,2,3,4 categoryname:eq:condiments 

at minute i'm using regex , strings find | ^ knowing if it's , or or regex matches brackets , works single item it's when nest values i'm struggling.

the regex looks like

@"\((.*?)\)" 

i need way of using regex match nested brackets , appreciated.

you transform string valid xml (just simple replace, no validation):

var output = filterstring     .replace("(","<node>")     .replace(")","</node>")     .replace("|","<andnode/>")     .replace("^","<ornode/>"); 

then, parse xml nodes using, example, system.xml.linq.

xdocument doc = xdocument.parse(output); 

based on comment, here's how rearrange xml in order wrapping need:

foreach (var item in doc.root.descendants()) {     if (item.name == "ornode" || item.name == "andnode")     {         item.elementsafterself()             .tolist()             .foreach(x =>             {                 x.remove();                 item.add(x);             });     } } 

here's resulting xml content:

<node>   <andnode>     <node>       <ornode>         <node>categoryid:eq:1,2,3,4</node>         <node>categoryname:eq:condiments</node>       </ornode>     </node>     <node>description:lk:”*and*”</node>   </andnode> </node> 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -