从命令提示符读取数据并将数据存储在字符串中

时间:2017-04-19 11:22:27

标签: c# cmd

Microsoft Windows [版本10.0.14393](c)2016 Microsoft Corporation。版权所有。 C:\ Program Files(x86)\ IIS Express> D:\ sample1.bat C:\ Program Files(x86)\ IIS Express> echo ECHO打开。 C:\ Program Files(x86)\ IIS Express> echo“Select * from Table”“Select * from Table”C:\ Program Files(x86)\ IIS Express> exit

我得到的输出如上,但我只需要“从表中选择*”

1 个答案:

答案 0 :(得分:0)

您可以使用Regular Expression来匹配所需字符串的部分。在这里,我正在寻找符合以下模式的任何字符:echo "{captured}"并捕获引号内的任何字符。

using System.Text.RegularExpressions;

string input = "Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved. C:\\Program Files (x86)\\IIS Express>D:\\sample1.bat C:\\Program Files (x86)\\IIS Express>echo ECHO is on. C:\\Program Files (x86)\\IIS Express>echo \"Select * from Table\" \"Select * from Table\" C:\\Program Files (x86)\\IIS Express>exit";

Regex findEcho = new Regex("echo (?<echostring>\".*\")", RegexOptions.Compiled);

var matches = findEcho.Matches(input);
foreach (Match match in matches) {
    if (match.Success) {
        if (match.Groups["echostring"].Success) {
            Console.WriteLine(match.Groups["echostring"].Value);
        }
    }
}

输出:"Select * from Table" "Select * from Table"

.net Fiddle