我正在尝试按照以下模式提取所有文本(显示为 xxxx):
Session["xxxx"]
使用 C#
这可能是 Request.Querystring["xxxx"]
所以我试图动态构建表达式。当我这样做时,我会遇到各种关于未转义字符或不匹配的问题:(
一个例子可能是:
string patternstart = "Session[";
string patternend = "]";
string regexexpr = @"\\" + patternstart + @"(.*?)\\" + patternend ;
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";
MatchCollection matches = Regex.Matches(sText, @regexexpr);
谁能帮我解决这个问题,因为我很困惑(因为我似乎总是使用 RegEx :))
最佳答案
对您的代码进行一些小的修改。
string patternstart = Regex.Escape("Session[");
string patternend = Regex.Escape("]");
string regexexpr = patternstart + @"(.*?)" + patternend;
关于c# - 2 个字符串之间文本的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602093/