Sign in to follow this  
athono

[.net] I have a regular expression (regex) question...

Recommended Posts

athono    132
I have a regular expression (regex) question...

How would I use regular expressions to remove the contents in parenthesis in a string in C# like this:

"SOMETHING (#2)"

The part of the string I want to remove always appears within paranthesis and they are always # followed by some number. The rest of the string needs to be left alone.


Share this post


Link to post
Share on other sites
athono    132
By using this:

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.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.

Share this post


Link to post
Share on other sites
j-locke    945
It looks like using the Replace method in that same Regex class you linked to would get the job done for you. Assuming your regular expression is already matching the correct part, you could replace it with and empty string.

[quote name='athono' timestamp='1310442226' post='4834122']


By using this:

[url="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx"]http://msdn.microsof...ions.regex.aspx[/url]

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.


[/quote]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this