0

I'm trying to learn how to use regex to remove all gibberish characters from a string except for alphanumeric and : . and the newline character, whatever it may be.

I am using this

TextBox1.Text = Regex.Replace(TextBox1.Text, "[^:.0-9a-zA-Z ]+?", "")

But my result seems to remove all the newline characters too, I thought this is what the ? at the end of the regex query did, tell it to exclude the newline character from the replacement?

Thanks for your input guys

2
  • are you using singleline mode.. Commented Oct 6, 2012 at 8:48
  • No, the text box is multiline ;) Commented Oct 6, 2012 at 8:50

1 Answer 1

2

It should be

[^:.0-9a-zA-Z\r\n]+

? is not required

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

3 Comments

I tried this but it didn't remove any of the gibberish characters
Two comments: You don't need to escape the dot inside a character class. And I would use \r\n instead of \s\n - \n is already a part of \s, and \s matches spaces and tabs, too, which may not be desired.
Perfect! Thanks very much guys. This is tricky to grasp at first think i'm going to buy a book on it.

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.