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

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.

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

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/

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;
        }
    }
}

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;
            }
        }

Edited 7 Years Ago by Geekitygeek: n/a

Comments
knows what he's doing

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!

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

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

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

Edited 7 Years Ago by ddanbe: n/a

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

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

This question has already been answered. Start a new discussion instead.