使用批处理文件,在另一个字符串中的子字符串匹配后获取 4 个字符?
我试图想出一种提取字符串中子字符串匹配后出现的 4 个字符的方法。我能想到的最接近的东西看起来效率非常低,甚至效果也不是很好。有谁知道更好的方法吗?
@echo off
cls
SET "_TMPVAR=Ports|http:8000|https:8443|"
SET _TMPVAR=%_TMPVAR:|=,%
FOR /F "tokens=1-3 delims=," %%m in ("%_TMPVAR%") do (
SET "_TMPVAR1=%%n"
SET "_TMPVAR2=%%o"
)
SET _HTTPS=0000
IF "%_TMPVAR1:~4,1%"=="s" (
SET "_HTTPS=%_TMPVAR1:~-4%"
)
IF "%_TMPVAR2:~4,1%"=="s" (
SET "_HTTPS=%_TMPVAR2:~-4%"
)
SET _
ECHO SSL Port is %_HTTPS%
pause
I am trying to come up with a method of extracting 4 characters that occur after a substring match in a string. The closest thing I could come up with is terribly inefficient looking and doesn't even work all that well. Does anyone know a better way of doing this?
@echo off
cls
SET "_TMPVAR=Ports|http:8000|https:8443|"
SET _TMPVAR=%_TMPVAR:|=,%
FOR /F "tokens=1-3 delims=," %%m in ("%_TMPVAR%") do (
SET "_TMPVAR1=%%n"
SET "_TMPVAR2=%%o"
)
SET _HTTPS=0000
IF "%_TMPVAR1:~4,1%"=="s" (
SET "_HTTPS=%_TMPVAR1:~-4%"
)
IF "%_TMPVAR2:~4,1%"=="s" (
SET "_HTTPS=%_TMPVAR2:~-4%"
)
SET _
ECHO SSL Port is %_HTTPS%
pause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改进它的一种方法是消除看似不必要的逗号替换:
此外,您可以利用分隔符列表来拆分
|
和:
分解您的方案和端口,允许您将逻辑放入FOR
语句中:使用分隔符行为的优点是它允许使用任意长度的端口号。
One way to improve it is to eliminate the seemingly unnecessary comma replacement:
Further, you can take advantage of the delimiter list to split on
|
and:
to break up your schemes and ports, allowing you to put the logic inside theFOR
statement:An advantage of using the delimiter behavior is that it allows for port numbers of any length.