If you mean that you want to get the text from a mutliline TextBox, and extract any text between an Start-of-group indicator and an End-of-group indictor, then it's pretty simple - just use a Regex:
public static Regex regex = new Regex(
"(?<=group \\()(?<GroupName>.*?)\\)(?<Data>.*?)(?=end group)",
RegexOptions.Singleline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
private void myButton_Click(object sender, EventArgs e)
{
MatchCollection ms = regex.Matches(textBox1.Text);
foreach (Match m in ms)
{
Console.WriteLine("{0} = \"{1}\"", m.Groups["GroupName"], m.Groups["Data"]);
}
}
"could you please explain this code to me? I am not very good with regex. I do not use it very much, and all the times i do use it, i just copy code from the internet"
Firstly, get a copy of
Expresso[
^] - it's free, and it examines and generates Regular expressions. it can explain then to you as well as help you create them.
Used correctly, Regexes are a wonderful tool!
Here is what it says about the above:
///
/// Regular expression built for C# on: Wed, May 30, 2012, 10:20:39 AM
/// Using Expresso Version: 3.0.3634, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// Match a prefix but exclude it from the capture. [group \(]
/// group \(
/// group
/// Space
/// Literal (
/// [GroupName]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Literal )
/// [Data]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// Match a suffix but exclude it from the capture. [end group]
/// end group
/// end
/// Space
/// group
///
What that means is that the regex is made up of 5 sections:
(?<=group \()
A prefix which is required, but not captured: "group ("
(?<GroupName>.*?)
Variable data which is captured as a data group called "GroupName", which consists of all characters up to the next section.
\)
A ")" to terminate the group name
(?<Data>.*?)
Variable data which is captured as a data group called "Data", which consists of all characters up to the next section.
(?=end group)
A suffix which is required, but not captured: "end group"
So what it does is look for text which starts with "group (xxx)" and ends with "end group" and stores the "xxx" as a name, and the rest as data.
All the C# code is doing is declaring the regex, and telling it to capture all matching data. It then loops though each set of matches and prints out the group name with it's related data.
Sorted!