5

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

Session["xxxx"]

using c#

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 :(

an example might be:

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);

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

1
  • I am trying to do the same thing for a VB project Session("xxxx") Commented Apr 15, 2016 at 5:24

3 Answers 3

7

With some little modifications to your code.

string patternstart = Regex.Escape("Session[");
string patternend = Regex.Escape("]");
string regexexpr = patternstart + @"(.*?)" + patternend;
Sign up to request clarification or add additional context in comments.

2 Comments

You forget "". Your pattern will match Session[] too
@SriramSakthivel Have you tested it before commenting? See the usage: var matches = Regex.Matches(sText, regexexpr).Cast<Match>().Select(m => m.Groups[1].Value).ToList();
1

The pattern you construct in your example looks something like this:

\\Session[(.*?)\\]

There are a couple of problems with this. First it assumes the string starts with a literal backslash, second, it wraps the entire (.*?) in a character class, that means it will match any single open parenthesis, period, asterisk, question mark, close parenthesis or backslash. You'd need to escape the the brackets in your pattern, if you want to match a literal [.

You could use a pattern like this:

Session\[(.*?)]

For example:

string regexexpr = @"Session\[(.*?)]";
string sText = "Text to be searched containing Session[\"xxxx\"] the result would be xxxx";

MatchCollection matches = Regex.Matches(sText, @regexexpr);
Console.WriteLine(matches[0].Groups[1].Value); // "xxxx"

Comments

1

The characters [ and ] have a special meaning with regular expressions - they define a group where one of the contained characters must match. To work around this, simply 'escape' them with a leading \ character:

string patternstart = "Session\[";
string patternend = "\]";

An example "final string" could then be:

Session\["(.*)"\]

However, you could easily write your RegEx to handle Session, Querystring, etc automatically if you require (without also matching every other array you throw at it), and avoid having to build up the string in the first place:

(Querystring|Session|Form)\["(.*)"\]

and then take the second match.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.