c# - 在长字符串中查找所有以 [ 开头并以 ] 结尾的字符串部分

标签 c# .net regex string algorithm

我有一个有趣的问题,我想找到一个我已经用 regex 尽力而为的最佳解决方案。我想要的是使用 C# 使用正则表达式或任何其他方法从该字符串中找到所有 col_x 值。

[col_5] is a central heating boiler manufacturer produce boilers under [col_6]
 brand name . Your selected [col_7] model name is a [col_6] [col_15] boiler.   
[col_6] [col_15] boiler [col_7] model [col_10] came in production untill 
[col_11].  [col_6] model product index number is [col_1] given by SEDBUK 
'Seasonal Efficiency of a Domestic Boiler in the UK'. [col_6] model have 
qualifier [col_8] and GCN  [col_9] 'Boiler Gas Council No'. [col_7] model 
source of heat for a boiler combustion is a [col_12].

期望的输出是一个数组

var data =["col_5","col_10","etc..."]

编辑

我的尝试:

string text = "[col_1]cc[col_2]asdfsd[col_3]";
var matches = Regex.Matches(text, @"[[^@]*]");
var uniques = matches.Cast<Match>().Select(match => match.Value).ToList().Distinct();

    foreach(string m in uniques)
    {
        Console.WriteLine(m);

    }

但没有成功。

最佳答案

尝试这样的事情:

string[] result = Regex.Matches(input, @"\[(col_\d+)\]").
                            Cast<Match>().
                            Select(x => x.Groups[1].Value).
                            ToArray();

关于c# - 在长字符串中查找所有以 [ 开头并以 ] 结尾的字符串部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32892066/

相关文章:

javascript - 正则表达式在 xml 标签中查找属性并替换其值(节点/Javascript)

regex - 正则表达式以匹配CSV分隔符

c# - 禁用 ToolbarItem Xamarin.Forms

c# - 错误 XDG0008 : NumberBox is not supported in a Universal Windows Platform project

c# - .net lambda 表达式和输出参数

.net - 使用 Dapper 从存储过程中获取字段名称

c# - 实体 orderby 的 linq

php - 文件路径的正则表达式

c# - 从远程计算机简单绑定(bind)到 AD-LDS 失败

c# - 任务与 AsParallel()