c# - 2 个字符串之间文本的正则表达式模式

标签 c# regex

我正在尝试按照以下模式提取所有文本(显示为 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/

相关文章:

c# - 多个arraylist重复subjectname顺序显示

c# - XAML 绑定(bind)问题 Windows Phone 中的 ImageSource

javascript - 使用 jquery 添加元素到空列表

python - 正则表达式内容生成器

c# - c#中的对象引用有点困惑

c# - 将新行添加到 datagridview 的底部

Java正则表达式和转义元字符

python re sub 与 findall

javascript - 正则表达式 - 忽略内部 word + html

c# - 正则表达式查找组内的值