I’m trying to write a C function to parse a MAC address input, with either spaces, colons or dashes as separators. I’ve been looking into using %*[-:] to match multiple characters but it doesn’t seem to be able to detect a white-space character (e.g. a space), and also it produces the wrong values with dashes as it’s think that the numbers are negative.
My function looks something like:
scanf("%2x%*[-:]%2x", &hex1, &hex2);
Is it possible in one scanf? Or are there other better ways? Thanks.
I don’t know how portable this is, or if it matches your requirements exactly.
With this input (tab after the first hex number, space before the last):
It produces this output:
If you need more than one item in the same call, try some variation of (takes spaces or tabs as separators):
Which gives this for the above input: