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

标签 c# regex

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

[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/

相关文章:

c# - Xamarin Resource.Designer.cs 不刷新新 ID 或事件

c# - 如何解决引用网站的请求验证 token

php - 从电话号码确定国家

正则表达式匹配所有排除 block 注释

regex - Perl 单词拆元音 : removing all vowels except the first and last

c# - WCF + Silverlight 4 : How to configure known types via Web. 配置?

C# 有抽象类和接口(interface),它也应该有 "mixins"吗?

c# - 检查 FileAttributes 枚举

regex - 如何匹配 perl6 语法中的十六进制数组

如果字符串包含 1 组或多组 1-10 字母数字,则匹配正则表达式