输入查询

 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/

10-09 01:49