使用RegEx从字符串中提取多个值

Ton*_*ola 2 .net c# regex

我有一个输入字符串,如下例所示:

string.Format("Document {0}, was saved by {1} on {2}. The process was completed 
{3} milliseconds and data was received.", 
"Document.docx", "John", "1/1/2011", 45);
Run Code Online (Sandbox Code Playgroud)

它会生成如下所示的字符串:

Document Document.docx, was saved by John on 1/1/2011. The process was completed 
45 milliseconds and data was received.
Run Code Online (Sandbox Code Playgroud)

一旦这样的字符串是从不同的应用程序接收,这将是与正则表达式解析和提取值的最简单的方法Document.docx,John,1/1/2011,45从它.

我正在寻找最简单的方法,因为我们将不得不解析许多不同的输入字符串.

cdh*_*wie 8

你可以使用这样的东西:

private static readonly Regex pattern =
    new Regex("^Document (?<document>.*?), was saved by (?<user>.*?) on " +
        "(?<date>.*?)\\. The process was completed (?<duration>.*?) " +
        "milliseconds and data was received\\.$");

public static bool MatchPattern(
    string input,
    out string document,
    out string user,
    out string date,
    out string duration)
{
    document = user = date = duration = null;

    var m = pattern.Match(input);
    if (!m.Success)
        return false;

    document = m.Groups["document"].Value;
    user = m.Groups["user"].Value;
    date = m.Groups["date"].Value;
    duration = m.Groups["duration"].Value;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

返回包含所需信息而不是使用out参数的复合类型可能值得重构.但这种方法仍然有效.

要使用此代码,您可以执行以下操作:

var input = "...";

string document, user, date, duration;
if (MatchPattern(input, out document, out user, out date, out duration)) {
    // Match was successful.
}
Run Code Online (Sandbox Code Playgroud)