开发者

String Tokenization/Regex Stack/Queue Problem - "ABC [YXYZ] [123] SomeText More Text [Split]"

how can i split them into an array of items, i.e. array[0] = "ABC ", array[1] = "YXYZ", array[2] = " " array[3]= " 123 " ...

Regular Expression Gurus! Help!

Don't care about performance, only code terseness, i.e. nifty unreadable code is totally fine.

EXTRA CREDIT:

I really want to do this --> everything in [ ] needs to basically formatted in a specific way. The caveat is that I can't do a regex replace, so I have to put into a collection, and somehow mark that I'm in plain text mode or decoration mode. I'm thinking I'll put this all into a queue or stack, and put a null in the s开发者_JS百科tack to identify marking plain text vs. decoration regions.

any ideas?


Do you need to use regex for this?

Using the string split function:

       Dictionary <string, bool> theDict = new Dictionary<string,bool> (); 
       string input= @"abc [xyz] [123] asdasd";
       string[] a2;
       string[] a1 = input.Split('[');
       for (int i = 0; i< a1.Length; i++)
       {
           a2 = a1[i].Split(']');
           if (a2.Length == 1)
               theDict.Add(a2[0], false);  // no special formatting
           else
           {
               theDict.Add(a2[0], true);   // special formatting

               theDict.Add(a2[1], false);
           }
       }

will get you the result you are asking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜