Sign in to follow this  

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

This topic is 2351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

This topic is 2351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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