I have been trying to parse a mac with format "xxxx[Delimiter]xxxx[Delimiter]xxxx" where "x" is in [0-9,A-F,a-f] and "[Delimiter]" is in [:,.,-]. I have tried the following code and have referred the example with title Regex to match a variable in Batch scripting and What is a regular expression for a MAC Address?.
set MACAddr=012a.23fa.5ffc
If [%MACAddr%] EQU [] (
echo MAC Address is not set. Please set it to proceed.
) else (
echo %MACAddr%|findstr /r "^([0-9A-Fa-f]{2}[:.-]?){5}([0-9A-Fa-f]{2})$"
if errorlevel 1 (echo Mac %MACAddr% is not of same Format as xxxx.xxxx.xxxx) else ( pause )
)
also tried this
echo %MACAddr%|findstr /r "^([0-9A-Fa-f]{4})([:.-])([0-9A-Fa-f]{4})([:.-])([0-9A-Fa-f]{4})$"
But it only runs if errorlevel 1 (echo Mac %MACAddr% is not of same Format as xxxx.xxxx.xxxx)
.Please tell me what am i doing wrong.
findstr
has a very limited subset of REGEX (see for /?
)
Your request can be formulated as:
findstr /ri "^[0-9A-F][0-9A-F][0-9A-F][0-9A-F].[0-9A-F][0-9A-F][0-9A-F][0-9A-F].[0-9A-F][0-9A-F][0-9A-F][0-9A-F]$"
where .
means "any char". If you want to break it down to just ;
, .
and -
as delimiters, use [;.-]
instead of .
. If the delimiters may be there or may not be there, use [;.-]*
instead (where *
means "zero or more" - sorry, there is no ?
"none or one" with findstr
.)