C# 仅在查找给定字符串后查找正则表达式

C# 仅在查找给定字符串后查找正则表达式,c#,C#,我正在尝试创建一个方法,该方法接收文本和字符串,并使用regex查找与给定字符串关联的日期时间 private static DateTime getLastExecutionTime(string text, string nameFile) { string lastRun = string.Empty; if (Regex.IsMatch(text, nameFile)) { lastRun = Regex.Match(text, "[0-9]{2

我正在尝试创建一个方法,该方法接收文本和字符串,并使用regex查找与给定字符串关联的日期时间

private static DateTime getLastExecutionTime(string text, string nameFile)
{
    string lastRun = string.Empty;

    if (Regex.IsMatch(text, nameFile))
    {
        lastRun = Regex.Match(text, "[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}").ToString();

        return DateTime.Parse(lastRun);
    }

    return nullDate;
}
我不知道正则表达式匹配的位置。因为文本是可编辑的,所以它可以在任何地方,并随时间变化。下面的示例有3个选项,但可以有10个、25个甚至100个

目前,我已经创建了创建datetime的方法,但是它是第一个匹配项,而不是给定字符串之后的匹配项

private static DateTime getLastExecutionTime(string text, string nameFile)
{
    string lastRun = string.Empty;

    if (Regex.IsMatch(text, nameFile))
    {
        lastRun = Regex.Match(text, "[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}").ToString();

        return DateTime.Parse(lastRun);
    }

    return nullDate;
}

如果日期识别器是文件名,则需要将其添加到正则表达式中。 在本例中,这个
Regex.Match
将找到一个后跟“Dog”的日期

或者以更一般的方式:

    var matches = Regex.Match(text, nameFile + " ([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9])");
在这种情况下,您需要访问匹配结果的“Group1”。索引为0的组将返回不带组的匹配项

matches[0].Groups[1].Value
有关MatchCollection的更多信息:

试试这个表达式:

Regex.Match(text, "^Dog (.+)$", RegexOptions.Multiline).Groups[1].Value
对于
文本
变量中的输入,输出为:
03-08-2019 12:32

如果需要将其参数化,请继续:

Regex.Match(text, $"^{query} (.+)$", RegexOptions.Multiline).Groups[1].Value
但请确保您从可信来源接收到
查询
,以防止注入攻击


您可以在此处快速测试表达式:

我们可以尝试使用以下模式进行单行正则表达式替换:

^[\s\S]*([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2})[\s\S]*$
脚本:

string text = "Cat 01-08-2019 16:32\r\nDog 03-08-2019 12:32";
string pattern = @"^[\s\S]*([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2})[\s\S]*$";
string output = Regex.Replace(text, pattern, "$1");
Console.WriteLine(output);
这张照片是:

03-08-2019 12:32

一个选项是使用Substring和IndexOf删除nameFile之前的所有文本

private static DateTime getLastExecutionTime(string text, string nameFile)
{
    string lastRun = string.Empty;

    if (Regex.IsMatch(text, nameFile))
    {
        lastRun = Regex.Match(text.Substring(text.IndexOf(nameFile)), " [0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}").ToString();

        return DateTime.Parse(lastRun);
    }

    return new DateTime();
}
您还可以使用完整的正则表达式解决方案:

private static DateTime getLastExecutionTime(string text, string nameFile)
{
    string lastRun = Regex.Match(text, "(?:" + nameFile + ") ([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2})").Groups[1].Value;

    if (string.IsNullOrEmpty(lastRun))
        return new DateTime();

    return DateTime.Parse(lastRun);
}

在这种情况下,最好是在没有正则表达式的情况下找到该行,并且在找到该行后使用正则表达式从该行传递日期。我并不真正想要最后一行,因为我可以有两个以上的名称。例如:“Cat 01-08-2019 16:32\r\nDog 03-08-2019 12:32\r\nCow 02-08-2019 10:50\r\nBear 18-07-2019 19:22”那么您想要哪一个?访问matchCollection[1]第二个例子。我真的不知道哪个是位置。这个例子有2个,我已经改为3个。但是可以有10100个。甚至可以加班。你我不明白你怎么知道哪个日期是相关的。与我相关的日期是给定字符串(nameFile)后面的日期我相信建议的第一个选项很好。将文本子串到给定的字符串,然后执行正则表达式。我不确定它是否更有效。我将在两个建议中运行一些测试。提前谢谢!
private static DateTime getLastExecutionTime(string text, string nameFile)
{
    string lastRun = Regex.Match(text, "(?:" + nameFile + ") ([0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2})").Groups[1].Value;

    if (string.IsNullOrEmpty(lastRun))
        return new DateTime();

    return DateTime.Parse(lastRun);
}