2个字符串之间的文本的正则表达式模式 [英] Regex pattern for text between 2 strings

查看:45
本文介绍了2个字符串之间的文本的正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照以下模式提取所有文本(显示为xxxx):

I am trying to extract all of the text (shown as xxxx) in the follow pattern:

Session["xxxx"]

使用c#

这可能是 Request.Querystring ["xxxx"] ,所以我试图动态构建表达式.当我这样做时,我会遇到有关未转义字符或不匹配的各种问题:(

This may be Request.Querystring["xxxx"] so I am trying to build the expression dynamically. When I do so, I get all sorts of problems about unescaped charecters or no matches :(

一个例子可能是:

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一样:))

Can anyone help with this as I am stumped (as I always seem to be with RegEx :) )

推荐答案

对代码进行一些修改.

string patternstart = Regex.Escape("Session[");
string patternend = Regex.Escape("]");
string regexexpr = patternstart + @"(.*?)" + patternend;

这篇关于2个字符串之间的文本的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆