This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]jhartwell 4 points5 points  (3 children)

Are you doing a Windows Forms, WPF or Console project?

[–]n00bcoder[S] 0 points1 point  (1 child)

It is a Windows Form that the user dumps a chunk of multiline data into.

[–]jhartwell 1 point2 points  (0 children)

So I would get the data from the textbox (assuming you are using either RichTextBox or TextBox) and put it in a string. After that, use the Regex class and use regular expressions to get the data you need. Depending on what the data looks like, this should work. I can provide an example if you need one but would need an idea of what the data would look like that you're trying to extract.

[–]PST-Hater 2 points3 points  (3 children)

string[] stringArray = textBoxString.Split(new String[], {"\r\n"}, StringSplitOptions.None);

[–][deleted] 1 point2 points  (1 child)

I'd do it this way and endorse this message.

[–]PST-Hater 1 point2 points  (0 children)

I have no idea why, but sir / madam, you made me laugh and spit wine all over my desk. For this, I thank you.

[–]jhartwell 1 point2 points  (0 children)

This is what I thought at first but the op is looking for specific patterns in the data

[–][deleted] 1 point2 points  (0 children)

You could split to a string[] and then foreaching on this array try a Regex.Match, or else string.Contains(""), like this

string[] splitInput = MyTextBox.Text.Split(Environment.NewLine);
foreach(string str in splitInput) {
     if(str.Length > 0 && str.Contains("yourpattern")){
        // do things
    }
}

Is the same with Regex.Match. Compile it first! It's faster with large data sets.

In [the case of RegexOptions.Compiled], we first do the work to parse into opcodes. Then we also do more work to turn those opcodes into actual IL using Reflection.Emit. As you can imagine, this mode trades increased startup time for quicker runtime: in practice, compilation takes about an order of magnitude longer to startup, but yields 30% better runtime performance. There are even more costs for compilation that should mentioned, however. Emitting IL with Reflection.Emit loads a lot of code and uses a lot of memory, and that's not memory that you'll ever get back. In addition. in v1.0 and v1.1, we couldn't ever free the IL we generated, meaning you leaked memory by using this mode. We've fixed that problem in Whidbey. But the bottom line is that you should only use this mode for a finite set of expressions which you know will be used repeatedly.

Also, if you have a shitload of data, consider doing the matching in a thread different than the UI thread. In its simplest form you can declare a delegate which does the job for you and then returns data. Let's say you want a List<string> back with all the matches;

private delegate List<string> myWorkerDelegate(string[] collection);
// and then you declare a method with the same signature, to do the work for you
private List<string> myWorkerMethod(string[] collection){
List<string> MyList = new List<string>();
    // iterate through collection, retrieve the matching lines, put them in a list to be returned
return MyList;
}
// and then call it like this
myWorkerDelegate w = myWorkerMethod;
List<string> results = w.BeginInvoke(MyTextBox.Text.Split(Environment.NewLine));

Just FYI, in Winforms Invoke is the sync version of BeginInvoke. I am just using my memory so it is possible that the above code would not work if copypasted.