格式为 xxxx.xxxx.xxxx 的 MAC 的批量正则表达式

Anu*_*uti 1 windows cmd batch-file findstr

我一直在尝试解析格式为“xxxx[Delimiter]xxxx[Delimiter]xxxx”的mac,其中“x”在[0-9,AF,af]中,“[Delimiter]”在[:,.,- ]。我已尝试以下代码,并引用标题为Regex 的示例来匹配批处理脚本中的变量以及什么是 MAC 地址的正则表达式?

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 )
)
Run Code Online (Sandbox Code Playgroud)

也尝试过这个

echo %MACAddr%|findstr /r "^([0-9A-Fa-f]{4})([:.-])([0-9A-Fa-f]{4})([:.-])([0-9A-Fa-f]{4})$"
Run Code Online (Sandbox Code Playgroud)

但它只能运行if errorlevel 1 (echo Mac %MACAddr% is not of same Format as xxxx.xxxx.xxxx)。请告诉我我做错了什么。

Ste*_*han 5

findstrREGEX 的子集非常有限(请参阅for /?

您的请求可以表述为:

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]$"
Run Code Online (Sandbox Code Playgroud)

其中.表示“任何字符”。如果您想将其分解为仅;,.-作为分隔符,请使用[;.-]代替.。如果分隔符可能存在或不存在,请改用[;.-]*(其中*表示“零个或多个” - 抱歉, 中没有?“无或一个” findstr。)