.net - 使用正则表达式分配变量

标签 .net regex string parsing

我正在寻找一种使用 C++ .NET 在正则表达式中使用模式分配变量的方法
就像是

String^ speed;
String^ size;

“命令速度=[速度]大小=[大小]”

现在我正在使用 IndexOf() 和 Substring() 但它很丑

最佳答案

String^ speed; String^ size;
Match m;
Regex theregex = new Regex (
  "SPEED=(?<speed>(.*?)) SIZE=(?<size>(.*?)) ",
  RegexOptions::ExplicitCapture);
m = theregex.Match (yourinputstring);
if (m.Success)
{
  if (m.Groups["speed"].Success)
    speed = m.Groups["speed"].Value;
  if (m.Groups["size"].Success)
    size = m.Groups["size"].Value;
}
else
  throw new FormatException ("Input options not recognized");

为语法错误道歉,我现在没有要测试的编译器。

关于.net - 使用正则表达式分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/98827/

相关文章:

c# - C# 中的正则表达式电子邮件验证器中的问题

java - 从字符串中提取每个单独的数字

python - 格式化python字符串中的换行符

Javascript RegEx 替换所有不在 HTML 标签内的字符

C 字符串到 float 的转换

c# - 与Flex交流USB设备,反之亦然

.net - 为什么(非通用)Stack 类实现为循环缓冲区? (这到底是什么意思)?

c# - 如何将 .net 属性应用于返回类型

.net - IDisposable,真的重要吗

regex - 如何在 XPath "contains"函数中使用正则表达式