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:
- how can I do that with regex?
- if I can do that with regex, can I use regex with linq?
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" }; }
}
You can use the static
RegEx.Split
method that takes two strings:string[] parts = RegEx.Split("A320-789-890", "-");
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?
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
精彩评论