开发者

How can I separate string array via REGEX?

I separated "A320-789-890" according to "-" and i get a list below:

"A101C", "B7CL", "E7CL", "D7CL"

Everything is ok. My result set above it is my so开发者_开发问答lution result. But i have 2 question:

  1. how can I do that with regex?
  2. if I can do that with regex, can I use regex with linq?
  3. which is more effective according to performance like my method below, or regex?

    namespace engRegex1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

        private void Form1_Load(object sender, EventArgs e)
        {
            Engineering eng = new Engineering();
            string[] engSplitList = new string[eng.engList.Count()];
            List<string> firstitem = new List<string>();
            foreach (string item in eng.engList)
            {
                engSplitList = item.Split('-');
                firstitem.Add(engSplitList[0]);
            }
            foreach (string item in firstitem)
                listBox1.Items.Add(item);
        }
    }
    public class Engineering
    {
        public List<string> engList = new List<string>() { 
            "A101C-234-456", "B7CL-567-789", "E7CL-567-789", "D7CL-567-789" };
    }
    

    }


  1. You can use the static RegEx.Split method that takes two strings:

    string[] parts = RegEx.Split("A320-789-890", "-");

  2. You can use Linq with this. You can also use Linq with the results from string.Split - these two things are not really connected. What do you want to do with Linq?

  3. You will need to test according to your own situation. I suggest you do a micro benchmark with representative data.

I would add that string.Split is simple and optimized. If it works you should just use it instead of looking for other solutions. Fix it if it becomes a bottleneck and a problem - chances are that if you profile your application you will find that the issues are elsewhere.


As long as you need to extract just the first part I'm suggesting IndexOf + Subsstring:

foreach (string item in eng.engList)
{
    listBox1.Items.Add(item.Subsstring(0, item.IndexOf('-'));
}

It would be the fastest (and probably the easiest) way.

//EDIT
with LINQ it would something like that:

listBox1.Items.AddRange(from item in eng.engList select item.Subsstring(0, item.IndexOf('-')));

with RegEX

foreach (string item in eng.engList)
{
    listBox1.Items.Add(RegEx.Match(item, "[^-]*").ToString());
}

with RegEX and LINQ:

listBox1.Items.AddRange(from item in eng.engList select RegEx.Match(item, "[^-]*").ToString())


You could also just use Linq and split to do what you want...

  var result = from s in engList
               let first =  s.Split('-').First()
               select first;


This is an roudabout way to do it, use AddRange instead, also bind the list to the listbox that way you would get a better performance and more compact code.

Judging from your level of expertise a regex is a bad fix for you, regex is hard and easy to do wrong, grab a book on regular expresions and then use.

Remember than compact notation is a tradeoff for readability

Regards

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜