0

I created two textboxes with "find" and "replace with" sort of combination. I then loop through cells in a DataGridView and check to see if it contains the value in the "find" box.

This has been working fine until I tried to find and replace "(" with "" empty string

This is the string it is looking for the "(" to find and replace in: The Hitch Hikers Guide To The Galaxy (S01xE06)

        string orig = (string)(dataGridView1.Rows[i].Cells["After"].Value);
        string newFilename = Regex.Replace(
orig, txtRenameFrom.Text, 
txtRenameTo.Text, 
RegexOptions.IgnoreCase);

Then I receive this error: parsing "(" - Not enough )'s.

3
  • 2
    If you want to allow your users to replace using regexes then obviously you have to handle the case where they supply invalid regexes, which is what happens here. If you don't want to allow regex replacement, why are you using Regex.Replace? Commented Oct 6, 2013 at 19:31
  • 2
    I think you want String.Replace and not Regex.Replace. That is not a valid regex expression in your example which is why an exception was thrown. If you want the user to be able to search and replace via their own Regexes, then you must add a try/catch to handle an exception gracefully. Commented Oct 6, 2013 at 19:31
  • The answer is clearly 42. Commented Oct 6, 2013 at 19:38

1 Answer 1

2

You're using Regex replace, ( is a special character in regular expressions. Either do a normal String.Replace or properly escape your regex.

Sign up to request clarification or add additional context in comments.

1 Comment

Regex.Escape could perhaps be used to escape the special chars input by the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.