gpt4 book ai didi

c# - 如何匹配表达式中的参数名称?

转载 作者:行者123 更新时间:2023-12-03 21:50:15 25 4
gpt4 key购买 nike

我有一组表达式,表示一些公式,其中包含一些参数。喜欢:

[parameter1] * [parameter2] * [multiplier]

还有许多其他人喜欢这样。

我想使用正则表达式,以便可以获得字符串列表 ( List<string> ),其中包含以下内容:

  • [paramter1]
  • [paramter2]
  • [multiplier]

我不经常使用正则表达式;如果您已经使用过类似的东西,如果您能分享,我将不胜感激。

谢谢!

最佳答案

这应该可以做到:

\[\w+\]

使用.net:

string s = "[parameter1] * [parameter2] * [multiplier]";
MatchCollection matches = Regex.Matches(s, @"\[\w+\]");

您可能想在此处使用捕获组:\[(\w+)\],以便在 Groups[1] 上获得参数名称。

关于c# - 如何匹配表达式中的参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2442140/

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