简体   繁体   English

如何设置转义<>符号删除定界符或字符串后右侧的文本

[英]How to set escape <> symbol Remove text to the right after a delimiter or string

Wanted to get string=abc 想要获取string = abc

set string=abc[Data]123[USER]Adam
set find=*[Data]

call set wanted=%%string:!find!=%%
echo %wanted%

set wanted=[Data]%wanted%
echo %wanted%

call set JOB_NAME=%%string:!wanted!=%%
echo %JOB_NAME%

output:
123[USER]Adam
[Data]123[USER]Adam
abc

Q1 Is there a easy way one line command to remove string to the right after a delimiter was found ? 问题1找到一个分隔符后,是否有一种简单的方法可以通过一行命令在右侧删除字符串? set find=*[Data] remove before the delimiter but not after delimiter.Tried to change the wild card to set find=[Data]* won't work at all. set find=*[Data]在定界符之前删除,但不能在定界符之后删除。试图将通配符更改为set find=[Data]*根本不起作用。

Q2 Try to set string in this format set string=abc<Data>123<USER>Adam even with escape character set string=abc^<Data^>123^<USER^>Adam the system prompt The system cannot find the file specified Q2尝试以这种格式set string=abc<Data>123<USER>Adam即使带有转义字符set string=abc^<Data^>123^<USER^>Adam系统提示符The system cannot find the file specified

The key, I believe, is quoting the strings 我相信关键是引用字符串

Still not clear WHAT you want to do, only how you've not been able to do it. 目前尚不清楚你想做的事,你如何只没能做到这一点

Try one or more of these, depending on your preference 根据您的喜好尝试其中的一种或多种

@ECHO Off
SETLOCAL
set "string=abc<Data>123<USER>Adam"
set "find=*<Data>"

call set "wanted=%%string:%find%=%%"
SET wanted

set "wanted=<Data>%wanted%"
SET wanted

call set "JOB_NAME=%%string:%wanted%=%%"
SET JOB_NAME

ECHO =====================================
set "string=abc<Data>123<USER>Adam"
set "find=*<USER>"

call set "wanted=%%string:%find%=%%"
SET wanted

set "wanted=<USER>%wanted%"
SET wanted

call set "wanted=%%string:%wanted%=%%"
SET wanted

set "find=*<Data>"
call set "JOB_NAME=%%wanted:%find%=%%"
SET JOB_NAME

ECHO =====================================
set "string=abc<Data>123<USER>Adam"
set "find=*<USER>"

call set "JOB_NAME=%%string:%find%=%%"
SET JOB_NAME

ECHO =====================================

set "string=abc<Data>123<USER>Adam"
FOR /f "tokens=1-5delims=<>" %%a IN ("%string%") DO SET "string1=%%a"&SET "string2=%%b"&SET "string3=%%c"&SET "string4=%%d"&SET "string5=%%e"

SET string

GOTO :EOF

Q1 Q1

I think you are talking about sub-string replacement -- correct me if I am wrong. 我认为您是在谈论子字符串替换 -如果我错了,请纠正我。
There is no reverse thing of %VARIABLE:*search:replac% , but there is a nice work-around, supposing the search part consists of a single character ( # in the example below) and it does not appear as the first character in the string: 没有%VARIABLE:*search:replac%反向操作,但是有一个不错的解决方法,假设search部分由单个字符组成(在下面的示例中为# ),并且它不作为第一个字符出现字符串:

set "VARIABLE=test#string"
for /F "delims=# eol=#" %%F in ("%VARIABLE%") do echo(%%F

This returns everything in %VARIABLE% before the first occurrence of the # character. 这将在首次出现#字符之前返回%VARIABLE%所有内容。 (The eol=# portion simply disables the default eol character ; .) eol=#部分仅禁用默认的eol字符; 。)

If the delimiter character might also appear at the first position in the string, precede a dummy character temporarily, other than the delimiter (like _ , for instance): 如果分隔符也可能出现在字符串的第一个位置,请暂时在虚拟字符之前,而不是分隔符(例如_ ):

set "VARIABLE=test#string"
for /F "delims=#" %%F in ("_%VARIABLE%") do (
    set "VALUE=%%F"
    setlocal EnableDelayedExpansion
    echo(!VALUE:~1!
    endlocal
)

If the search portion consists of multiple characters, things become a bit more complicated, but you could replace it by a single character that does not appear in the part before and use one of the above solutions. 如果search部分由多个字符组成,事情会变得有些复杂,但是您可以将其替换为之前未出现在该部分中的单个字符,并使用上述解决方案之一。

Q2 Q2

Magoo already shows the best way to avoid such errors in his answer . 脱线已经具备了避免这些错误的最好方式他的答案

I want to show you why the escaping failed in your approach: 我想向您展示为什么转义失败了:
The escaping like ^< and ^> is consumed by setting the variable with set . 通过使用set设置变量可以使用^<^>这样的转义。 When expanding it like %VARIABLE% , it appears unescaped, that is why you receive an error. 当像%VARIABLE%一样展开它时,它似乎没有转义,这就是为什么您会收到错误消息。 But -- besides quotation -- there are work-arounds: 但是,除了报价外,还有一些变通方法:

rem // This sets the variable to `abc<Data>123` literally:
set VARIABLE=abc^<Data^>123
rem // This fails as it expands to `abc<Data>123`:
echo(%VARIABLE%

setlocal EnableDelayedExpansion
rem /* This succeeds dueto delayed expansion, because
rem    special characters are no longer recognised then: */
echo(!VARIABLE!
endlocal

rem // This sets the variable to `abc^<Data^>123` literally:
set VARIABLE=abc^^^<Data^^^>123
rem /* This succeeds as it expands to `abc^<Data^>123`,
rem    hence the poisonous characters are escaped properly: */
echo(%VARIABLE%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM