Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hey guys,

I am now starting on working with textboxes and I want to be able to group specific lines of a textbox into 1 string. I don't know even if this is possible, but I want to try. This is an example textbox code:

group (groupname)
     (text goes here)
end group


I want to be able to group the text between the group name into a string. Do you know how to do this? Please help!
Posted
Comments
Sergey Alexandrovich Kryukov 29-May-12 18:43pm    
What's the problem? And you did not specify how exactly groups are defined or recognized, what's the expected result.
You can use "Improve question" above, add descriptive examples if ins and outs, etc.
--SA

1 solution

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:

C#
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)
    {
    // Capture all Matches in the InputText
    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!
 
Share this answer
 
v2
Comments
Member 8378691 30-May-12 5:13am    
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
Member 8378691 1-Jun-12 3:10am    
this isn't exactly what i was looking for...it is still good, but i want to store the data as a string. so lets say something like:

string 'something' = *this is where the Regex is used, this is the data stored between 'group(xxx)' and 'end group'*
OriginalGriff 1-Jun-12 3:49am    
If you only have the one, then change the regex slightly to discard the group name and just use the single value:
public static Regex regex = new Regex(
"(?<=group \\(.*?\\))(?.*?)(?=end group)",
RegexOptions.Singleline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
...
string s = regex.Match(textBox1.Text).Value;
Member 8378691 1-Jun-12 18:36pm    
I need to have more than just one group though. am I allowed to do something like:

public static Regex regex = new Regex(
"(?<=group " & stringname & " \\(.*?\\))(?.*?)(?=end group)",
RegexOptions.Singleline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled );
...
string s = regex.Match(textBox1.Text).Value;

that is what I am looking for, but I don't know if that will work.
Member 8378691 2-Jun-12 0:42am    
hey,

I just tried out the code and i realised that this is in C#, i am programming this in Visual Basic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900