GSharper

Friday, 30 March 2007

Substrings with regular expression

Hi,

since I always forget this one, I'll post it here.

I'm tired of always using native c# code to perform substring selects on string values. This produces lots of code. I've replaced it with regular expressions

Let's suppose we have the following string value

string expr = "This is a test message where I want to extract the value test out of [test]";

We can use this code:

string value=
Regex.Match(expr ,@"(?<=\[).*(?=\])").Value;

value will be test !!

Labels:

10 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home