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 !!
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: Regular Expressions
10 Comments:
When we have multiple values between [] then we can use this regex
MatchCollection paramValues = Regex.Matches(selectedMessage, @"(?<=\[).*?(?=\])");
Now loop through the matches in the collection to find the enclosed values
By GSharp, At 20 April 2007 at 13:49
Thanks ! Quick, helpful post.
By Anonymous, At 30 September 2008 at 09:46
I want filename without extention name how to do it??
By Suren, At 6 August 2009 at 10:32
Great post. Thanks for keeping it simple.
By Ryan, At 28 July 2011 at 18:55
Thanks - loop matches worked great!
By Anonymous, At 25 August 2011 at 21:45
Аpprеciаte thіѕ poѕt.
Let me try it οut.
Hаve a look аt my blog; näsplastik
By Anonymous, At 15 April 2013 at 01:44
Just want to say your article is as astonishing. The clarity on your post is simply excellent
and i could suppose you're knowledgeable on this subject. Fine with your permission let me to grasp your feed to keep updated with approaching post. Thanks 1,000,000 and please keep up the enjoyable work.
Visit my web blog view
By Anonymous, At 10 May 2013 at 00:57
Hi, Neat post. There's an issue together with your website in web explorer, would test this? IE still is the market chief and a huge part of other people will miss your magnificent writing because of this problem.
Here is my website free jackpot 6000
By Anonymous, At 17 June 2013 at 17:09
Here's a blog which benchmarks several different C# techniques to find the occurrence of a string within a string. Makes for a great read, especially for those who are looking for the *fastest* way:
http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-string-occurs-within-a-string
By Anonymous, At 27 August 2013 at 01:03
e cigarette, electronic cigarette, e cigarette, smokeless cigarette, electronic cigarettes, vapor cigarette
By Anonymous, At 29 November 2014 at 14:28
Post a Comment
Subscribe to Post Comments [Atom]
<< Home