954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

check string for illegal characters

Hi
I receive ascii data from the serial port which consists of only 0 to 9, /005, /004 and spaces between values, but sometimes the harware get spikes from the electric motors and then the recorder add some funny characters.
I need to check the string PortData = "" to see that only the above exists and nothing else.
I can check the string for the above characters, but how do you see if something else is in there?

thanks

procomp65
Light Poster
48 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Use if-else if or switch- case constructs to check for the right chars. This also seems something that can perfectly be handled by a Regex.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

the logic on how to do it, evades me. And I think I must check for IsNumbers and then "/004" "/005"

PortData = "1 2 3 4 5 6 7 8 9 0 /004 /005 /234 234 @ %";
            int index = PortData.IndexOf("1","2","3","4","5","6","7",
"8","9","0","/004","/005");
            if (index > -1)
            {

             }
else
{
MessageBox.Show("Data Corrupt);
}


How is this going to see if there is illegal char's?
Sorry, I do not have a clue hoe to do it

procomp65
Light Poster
48 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

the logic on how to do it, evades me. And I think I must check for IsNumbers and then "/004" "/005"

PortData = "1 2 3 4 5 6 7 8 9 0 /004 /005 /234 234 @ %";
            int index = PortData.IndexOf("1","2","3","4","5","6","7",
"8","9","0","/004","/005");
            if (index > -1)
            {

             }
else
{
MessageBox.Show("Data Corrupt);
}

How is this going to see if there is illegal char's? Sorry, I do not have a clue hoe to do it

Use Regex to search for legal and illegal patterns in your string. Here is a brief description of doing this: Regular Expressions Usage in C#

You might also find this useful: http://www.regular-expressions.info/

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

I wrote this little console application, hope it helps a bit, I know to little about Regex to give you info about that.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {
            // 'A' is an 'illegal' character here
            // WriteLine should print false.
            string S = "12A3";
            Console.WriteLine(CheckString(S));
            Console.ReadKey();
            return 0;
        }

        static bool CheckString(string PortStr)
        {
            bool result = false;
            char[] Charray = PortStr.ToCharArray();
            for (int i = 0; i < Charray.Length; i++)
            {
                switch (Charray[i])
                {
                    case '1':
                    case '2':
                    case '3':
                    case '4': result = true;
                        break;
                        //etc.
                    default: 
                        result = false;
                        return result;
                }
            }
            return result;
        }
    }
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Hi,

I would seriously recommend checking out some of the many tutorials on Regular Expressions. It is a powerful tool to have under your belt.

Heres a function that will check your data and only allow the valid characters you listed. It will return false if there are double digits(if they arent preceeded by a '/'), non-numerics or any '/'s if not followed by 004 or 005.

Hope this helps :)

private bool ValidInput(string input)
        {
            const string regExpr = @"[^\d\s]|(\d*?(?<!\/|\/\d)([0-9]{2})|(\/(?!00(4|5))))";

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, regExpr);
            if (m.Success)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Hi,

I would seriously recommend checking out some of the many tutorials on Regular Expressions. It is a powerful tool to have under your belt.

Heres a function that will check your data and only allow the valid characters you listed. It will return false if there are double digits(if they arent preceeded by a '/'), non-numerics or any '/'s if not followed by 004 or 005.

Hope this helps :)

private bool ValidInput(string input)
        {
            const string regExpr = @"[^\d\s]|(\d*?(?<!\/|\/\d)([0-9]{2})|(\/(?!00(4|5))))";

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, regExpr);
            if (m.Success)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


I would probably have spent half a day trying to come up with that pattern--LOL. You seem to be quite adept at creating regex patterns Ryshad. He should +rep you if it works!

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

me? adept? naah! lol. I did a little bit of work with reg's a couple years ago, but nothing in depth. I just pieced this together with an online syntax reference and an online tester :p

Bit of trial and error and i was there in no time

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Hi Guys

Thanks for all the quick responses. Ryshad. It looks as if your code will do the trick, but I think I might have been a bit vaque on the details. There are are double digit values in the data string. below is a real example of the data string
01 1 2 1 0340 16 01 14 06 08 20 09 0286 0010 0291 0611 0538 0600 0227 0600 0217 0603 0498 0409 0567 0475 0588 0517 0593 0543 0596 0559 0600 0570 0603 0579 0607 0586 0609 0591 0610 0595 0610 0597 0611 0599 0610 0600 0610 0601 0609 0602 0609 0602 0610 0602 0609 0603 0609 0603 0608 0603 0608 0603 0607 0603 0607 0603 0606 0603 0606 0602 0605 0602 0605 0602

Of regex I know nothing, so it does not make sense now

Thanks
Wilco

procomp65
Light Poster
48 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Looks like a set of unicode codes, most of them Arabic but not printable?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

no it's number values with /004 in the beginning and /005 inbetween

procomp65
Light Poster
48 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

is unicode 0004
is unicode 0005
All the rest seem to me like they are unicodes. See http://www.tamasoft.co.jp/en/general-info/unicode.html

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

wow..regex with a headache = eviiiiil lol

but i pushed through, i hate to be beaten by a challenge.
Turns out i was comin at it all wrong, the result was pretty straight forward:

//replace
const string regExpr = @"[^\d\s]|(\d*?(?<!\/|\/\d)([0-9]{2})|(\/(?!00(4|5))))";

//with
const string regExpr = @"((?![0-9\s\/]).)|\/(?!00(4|5))";


This will match any character that is not '0-9' a white space or a '/' followed by 004 or 005.

Remember to mark the thread as solved if this has answered your question :D

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Thanks. It did what I needed.

procomp65
Light Poster
48 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Glad to be of service ;)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: