New/unknown behaviour in percent/delayed expansion
Posted: 22 Jun 2016 01:54
Unbelievable
From the post [url="http://www.dostips.com/forum/viewtopic.php?f=3&t=7233#p47191"]One-liners for logical OR operator[/url]
First I thought, that it's a simple case where the parser rules can explain it.
But, thats not true it's a new behaviour.
I have to expand the percent/delayed expansion rules
When a variable replace expression begins with any character, except a percent, then the search pattern will be build up to the the first equal sign.
For the question marks is any character allowed, except the equal sign, but even percent signs.
But in the case you use percent signs in the search pattern, they will not expand anything, they are handled as normal characters.
The same rules applies to delayed expansion for the exclamation marks.
It's still not possible to replace a single percent sign with this behaviour, as you always need a prefixed character.
But now it's possible to replace all character up to the first percent sign.
jeb
From the post [url="http://www.dostips.com/forum/viewtopic.php?f=3&t=7233#p47191"]One-liners for logical OR operator[/url]
penpen wrote:Probably not as you (sambul35) expected (you should use delayed expansion and replace the outer percentage characters with exclamation marks; i've also added a semicolon),
but it is the first time i saw three percentage characters in one variable working:
Code:
:: test.bat
@echo off
setlocal enableDelayedExpansion
set "cities=;York;London;Brussels;%%1;%%2;%%3;"
echo %cities:;%2;=;%
echo !cities:;%2;=;!
endlocal
goto :eof
Code:
Z:\>test.bat London London
;York;London;Brussels;%1;%3;
;York;Brussels;%1;%2;%3;
Why does this work?
First I thought, that it's a simple case where the parser rules can explain it.
But, thats not true it's a new behaviour.
I have to expand the percent/delayed expansion rules
When a variable replace expression begins with any character, except a percent, then the search pattern will be build up to the the first equal sign.
Code: Select all
%var:A??????=replace%
For the question marks is any character allowed, except the equal sign, but even percent signs.
But in the case you use percent signs in the search pattern, they will not expand anything, they are handled as normal characters.
The same rules applies to delayed expansion for the exclamation marks.
Code: Select all
set "var1=Begin #%%~ End"
echo 1: %var1:#%~=Invalid%
set var2=#Begin #%%X%% End
set "X=Begin"
echo 2: %var2:#%X%=REPLACE%
output wrote:1: Begin Invalid End
2: #Begin REPLACE End
It's still not possible to replace a single percent sign with this behaviour, as you always need a prefixed character.
But now it's possible to replace all character up to the first percent sign.
Code: Select all
set "var=One percent%% sign"
echo %var:*%=No%
jeb