gpt4 book ai didi

c# - 如何使用C#从下面的字符串中提取键

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:01 25 4
gpt4 key购买 nike

输入查询

 select * from mytable where projectname = __$ProjectName$__ and  projectid = __$ProjectId$__ and env = __$EnvType$__


我想要以下输出的字符串( List<string>)列表。(双下划线+美元+“字符串” +美元+双下划线)
语言:C#

 __ $ProjectName$__
__ $ProjectId$__
__ $EnvType$__

最佳答案

尝试正则表达式;如果键


__$开始
包含标识符(以字母A..Za..z开头,可以包含字母或\和数字A..Za..z0..9
$__结尾


匹配的模式是__\$[A-Za-z]+[A-Za-z0-9]*\$__

码:

using System.Text.RegularExpressions;

...

string source =
"select * from mytable where projectname = __$ProjectName$__ and projectid = __$ProjectId$__ and env = __$EnvType$__";

List<string> keys = Regex
.Matches(source, @"__\$[A-Za-z]+[A-Za-z0-9]*\$__")
.OfType<Match>()
.Select(match => match.Value)
.ToList();

Console.Write(string.Join(Environment.NewLine, keys));


结果:

__$ProjectName$__
__$ProjectId$__
__$EnvType$__

关于c# - 如何使用C#从下面的字符串中提取键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258153/

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