将子字符串替换为延迟的扩展变量 [英] Replace a substring with delayed expended variable

查看:63
本文介绍了将子字符串替换为延迟的扩展变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过比较原始字符串和替换字符串(从中扣除子字符串)来计算多个变量.它在具有常规变量%var%的情况下确实起作用,并且不需要使用延迟扩展,但是有一种方法可以使它与延迟扩展变量!var!一起使用.而不是使用第二个FOR循环?以下代码无法计算pat#变量.我确实知道,可以使用其他方法对它们进行计数,但这只是一个示例,用于说明我有关替换子字符串的问题.

I'm trying to count a number of variables by comparing original string with a replacement string, from which a substring is deducted. It does work in cases with regular variables %var% and where use of delayed expansion is not required, but is there a way to make it work with delayed expansion variable !var! instead of using a 2nd FOR loop? The below code can't count pat# variables. I do realize, they can be counted using a different method, but this is just an example to illustrate my question about replacing substrings.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "pat1=A" & set "pat2=B" & set "pat3=C" & set "pat4=D" & set "pat5=E"
set "pat6=F" & set "pat7=G" & set "pat8=H" & set "pat9=I" & set "pat10=J"
set "patn=;0;1;2;3;4;5;6;7;8;9;"

if not defined patc (
    for /f %%l in ('set pat') do (
        set "patl=%%l" & set "patl=!patl:~3,1!"
        if not "!patn:;!patl!;=!"=="%patn%" (set /a "patc+=1" & echo !patc!)))
exit /b

推荐答案

您需要使用某种双扩展.
CALLFOR-parameter可能

You need to use some sort of double expansion.
There are CALL or FOR-parameter possible

....
set "patl=%%l" & set "patl=!patl:~3,1!" & echo ;!patl!;
call echo %%patn:;!patl!;=%%

or

FOR /F "delims=" %%S in ("!patn!") do (
    echo !patn:;%%S;=!
)

这篇关于将子字符串替换为延迟的扩展变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆