C# Regexp。用引号中的最后一个单词分隔由三个单词组成的字符串
请看,我该如何用C#表示正则表达式,这是一个由三个单词组成的序列,最后一个单词用双引号括起来。Example.from字符串应为隔离子字符串: 设置外部“信任Gi” 。我有这样一个代码,但常规exp是错误的C# Regexp。用引号中的最后一个单词分隔由三个单词组成的字符串,c#,regex,C#,Regex,请看,我该如何用C#表示正则表达式,这是一个由三个单词组成的序列,最后一个单词用双引号括起来。Example.from字符串应为隔离子字符串: 设置外部“信任Gi” 。我有这样一个代码,但常规exp是错误的 private void button1_Click(object sender, EventArgs e) { OpenFileDialog opendialog = new OpenFileDialog(); if (opendia
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
if (opendialog.ShowDialog() == DialogResult.OK)
{
var lines = File.ReadLines(opendialog.FileName);
string pattern = @"set vrouter ".*"";
foreach (var line in lines)
{
var matches = Regex.Matches(line, pattern);
foreach (Match match in matches)
{
if (match.Success)
textBox1.AppendText(match.Value + '\n');
}
}
}
给定输入设置vrouter“Trust Gi”
,匹配组将包含以下内容:
// m.Groups[1].Value == set
// m.Groups[2].Value == vrouter
// m.Groups[3].Value == Trust-Gi
下面是用解释性注释扩展的相同代码:
Match m = Regex.Match(input, @"
(\w+) # one or more word characters; captured into group 1
\s+ # one or more spaces
(\w+) # one or more word characters; captured into group 2
\s+ # one or more spaces
""([\w-]+)"" # one or more word characters or dash, surrounded by double-quotes; captured into group 3
", RegexOptions.IgnorePatternWhitespace);
编辑:
Match m = Regex.Match(input, @"set vrouter ""([\w-]+)""");
然后路由器名称将在
m.Groups[1]中。Value
这样的regexp输出所有类型的序列(somthing\u word somthing\u word“something\u word”)/但我需要精确设置vrouter“something word”。非常感谢。添加了更具体的正则表达式。
Match m = Regex.Match(input, @"set vrouter ""([\w-]+)""");