Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > C# 4.0 aka C# 2010 General Discussion
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2011, 09:46 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default How would I use regular expressions to remove the contents in parenthesis in a string

How would I use regular expressions to remove the contents in parenthesis in a string in C# like this:
"SOMETHING (#2)"
 
Old July 11th, 2011, 11:39 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default

By using this:
http://msdn.microsoft.com/en-us/libr...ons.regex.aspx
I have gotten some results like this:
string pattern = @"\(#\d+\)";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(interesting_string);
But I do not know what to do with the matches or what to do after this.
 
Old July 12th, 2011, 12:56 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Bill

Matches would find the occurrences of the given pattern. For your 'stripping' you need to use Replace to replace the match with nothing

Code:
string pattern = @"\(#\d+\)";
string interesting_string = "SOMETHING (#2)";
Regex rgx = newRegex(pattern, RegexOptions.IgnoreCase); 
MatchCollection matches = rgx.Matches(interesting_string);
string removed_String = rgx.Replace(interesting_string, "");

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions and string matching DennyLoi Java Basics 0 November 15th, 2007 01:37 PM
Help with Regular Expressions WestRowOps Other Programming Languages 1 May 18th, 2007 05:34 AM
Regular Expressions The_Iceman ASP.NET 2.0 Basics 0 September 29th, 2006 05:06 AM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM
Regular Expressions Dave Doknjas C# 1 August 9th, 2003 12:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.