将文本框的特定行分组为字符串 [英] Group Specific Lines of a TextBox into a String

查看:76
本文介绍了将文本框的特定行分组为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我现在开始使用文本框,并且希望能够将文本框的特定行分组为1个字符串.我不知道这是否可行,但我想尝试.这是一个示例文本框代码:

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!

推荐答案

如果您是想从多行文本框中获取文本,并提取组起始指示符和结束之间的任何文本,组指示器,那么非常简单-只需使用正则表达式即可:

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)
    {
    // 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"]);
        }
    }





您能否向我解释此代码?我对正则表达式不太满意.我使用它的次数不多,而且每次我都使用它时,我只是从互联网上复制代码".

首先,获取一份 Expresso [





"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 \()

是必需但未捕获的前缀:"group("

A prefix which is required, but not captured: "group ("

(?<GroupName>.*?)

可变数据,该数据被捕获为名为"GroupName"的数据组,其中包括直到下一节的所有字符. >

Variable data which is captured as a data group called "GroupName", which consists of all characters up to the next section.

\)

A)"终止组名

A ")" to terminate the group name

(?<Data>.*?)

可变数据,被捕获为名为"Data"的数据组,该数据组包含下一节之前的所有字符.

Variable data which is captured as a data group called "Data", which consists of all characters up to the next section.

(?=end group)

必需但未捕获的后缀:"end group"

因此,它的作用是查找以"group(xxx)"开头,以"end group"结尾,然后将"xxx"作为名称存储,其余作为数据存储的文本.

C#代码所做的所有事情就是声明正则表达式,并告诉它捕获所有匹配的数据.然后,它循环遍历每组匹配项,并打印出带有其相关数据的组名.

排序!

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!


这篇关于将文本框的特定行分组为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆