0

Update: The acceptable format is ADD|| . I need to check if the request that the server gets, is in this format, and the numbers are between <>. After that I have to read the numbers and add them and write the result back. So, if the format not fits to for example ADD|<5>|<8> I have to refuse it and make a specific error message(it is not a number, it is wrong format, etc.). I checked the ADD| part, I took them in an array, and I can check, if the numbers are not numbers. But I cannot check if the numbers are in <> or not, because the numbers can contain multiple digits and ADD|<7>|<13> is not the same number of items likeADD|<2358>|<78961156>. How can I check that the numbers are in between <>?

please help me with the following: I need to make a server-client console application, and I would like to validate requests from the clients. The acceptable format is XXX|<number>|<number>. I can split the message like here:

string[] messageProcess = message.Split('|');

and I can check if it is a number or not:

if (!(double.TryParse(messageProcess[1], out double number1)) || !(double.TryParse(messageProcess[2], out double number2)))

but how can I check the <number> part? Thank you for your advice.

7
  • 1
    I'm not sure if I understand your question - you are already trying to parse the numbers. Do you need to validate the 1st part? Could you edit your question and give us examples of both valid and invalid inputs? Commented Nov 7, 2018 at 14:38
  • I am not sure I am following your question. Aren't you already checking the number inside the if? Commented Nov 7, 2018 at 14:38
  • 1
    "but how can I check the <number> part?" Can you clarify what you want to check it for, other than it being a number? Commented Nov 7, 2018 at 14:39
  • ok ADD|<a>|<b> is a valid format Commented Nov 7, 2018 at 14:44
  • I have to validate, if it is a number, i have to validate if it begins at ADD, I have to validate, if it contains | in a right place etc. Commented Nov 7, 2018 at 14:45

1 Answer 1

1

You can use Regex for that.

If I understood you correctly, follwing inputs should pass validation:

xxx|1232|32133
xxx|5345|23423
XXX|1323|45645

and following shouldn't:

YYY|1231|34423
XXX|ds12|sda43

If my assumptions are correct, this Regex should do the trick:

XXX\|\d+\|\d+

What it does?

  • first it looks for three X's... (if it doesn't matter if it's uppercase or lowercase X substitute XXX with (?:XXX|xxx) or use "case insensitive regex flag" - demo)
  • separated by pipe (|)...
  • then looks for more than one digit...
  • separated by pipe (|)...
  • finally ending with another set of one or more digits

You can see the demo here: Regex101 Demo

And since you are using C#, the Regex.IsMatch() would probably fit you best. You can read about it here, if you are unfamiliar with regular expressions and how to use them in C#.

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

3 Comments

if (message.StartsWith("ADD|")) { string[] messageProcess = message.Split('|'); if (messageProcess.Length != 3) // string task = messageProcess[0].ToUpper(); { iro.WriteLine("ERR|Wrong format"); iro.Flush(); return true; }
The problem is, that I need to check ADD|<a>|<b> this format. This is the acceptable, with the <number> part
@Seine please update your question then with expected input (as it literally is, no placeholders)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.