0

我有一个完全像这样定义的字符串扩展:

public static string GetStringBetween(this string value, string start, string end)
{
    start = Regex.Escape(start);
    end = Regex.Escape(end);

    GroupCollection matches = Regex.Match(value, start + @"([^)]*)" + end).Groups;

    return matches[1].Value;
}

但是当我这样称呼时:

string str = "The pre-inspection image A. Valderama (1).jpg of client Valderama is not...";
Console.WriteLine(str.GetStringBetween("pre-inspection image ", " of client"));

它不写任何东西。但是当str值是这样的时候:

string str = "The pre-inspection image A. Valderama.jpg of client Valderama is not...";

它工作正常。为什么会这样?

我的代码是 C#,框架 4,在 VS2010 Pro 中构建。

请帮忙。提前致谢。

4

1 回答 1

2

因为您指定排除)正则表达式捕获组中[^)]的字符:@"([^)]*)"

并且由于)出现在第一个字符串:Valderama (1).jpg中,它将无法匹配。

你可能想要@"(.*)"

于 2012-06-11T10:11:26.543 回答