Regular Expression - Unprintable Character

G

Guest

Hello, I have an file that I am reading and it has some goffy characters in
it and I want to use a regular expression to clean up those characters. I am
not sure how to construct a regular expression which find these characters
and replace them with spaces. I am guessing that a regular expression which
would find the "good" values would look something like this..

Regex expr = new Regex (@"\w()\.,!@#$%<>" - but I am looking for anything
which is the oposite of that, as if I find something which is not in this
list, I want to replace it with spaces. (i.e expr.Replace("MyText|", " ");
)

I was thinking of using the following, but how to I tell it all the other
characters it should exclude from the match? Is there a "NOT" instruction?
Regex regexpr = new Regex (@"\W");

Hopefully this make sense...

Thanks in advance for your assistance!!!
 
T

Thomas Richter

Jim said:
Is there a "NOT" instruction?
[abc] matches a or b or c
[^abc] anything but a, b or c

Mit freundlichen Grüßen aus Kiel

Thomas Richter
 
L

Larry Lard

Jim said:
Hello, I have an file that I am reading and it has some goffy characters in
it and I want to use a regular expression to clean up those characters. I am
not sure how to construct a regular expression which find these characters
and replace them with spaces. I am guessing that a regular expression which
would find the "good" values would look something like this..

Regex expr = new Regex (@"\w()\.,!@#$%<>" - but I am looking for anything
which is the oposite of that, as if I find something which is not in this
list, I want to replace it with spaces. (i.e expr.Replace("MyText|", " ");
)

I was thinking of using the following, but how to I tell it all the other
characters it should exclude from the match? Is there a "NOT" instruction?

Yes!

Just as [abcd] matches a single character which is one of a b c or d,
[^abcd] matches a single character which is NOT one of a b c or d. So
given that

\w()\.,!@#$%<>

are your 'good' characters,

[^\w()\.,!@#$%<>]

will match any 'bad' character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top