【问题标题】:How to get a substring with special characters in batch file?如何在批处理文件中获取带有特殊字符的子字符串?
【发布时间】:2020-07-26 14:52:42
【问题描述】:

我正在尝试删除包含特殊字符的字符串的一部分。

字符串看起来像:<abc>123</abc> 我只想提取123

我试过了

set substr=<abc>
%my_string:substr=%

并按位置删除字符

set result=%my_string:~5,-6%

但两者都不起作用。有人可以帮助我吗?

ps。我是一批新人。 对不起英语。

【问题讨论】:

  • for /F "TOKENS=2 delims=&lt;&gt;" %%G in ("%substr%") do echo %%G

标签: string batch-file special-characters


【解决方案1】:

延迟扩展和字符串的双引号是这里需要的方法。

for 循环的使用只是在一行中定义要删除的所有子字符串的一种方式。

@echo off

Setlocal enabledelayedexpansion
set "string=<abc>123</abc>"
For %%A in ("<abc>" "</abc>") do Set "string=!string:%%~A=!"
Echo(!string!
pause

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2018-11-22
    • 1970-01-01
    • 2016-02-25
    • 2010-11-19
    • 1970-01-01
    相关资源
    最近更新 更多