gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:07 26 4
gpt4 key购买 nike

我有一个有趣的问题,我想找到一个我已经用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com