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

Started by
1 comment, last by j-locke 12 years, 9 months ago
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.


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

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.




By using this:

http://msdn.microsof...ions.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.


This topic is closed to new replies.

Advertisement