如何在Windows批处理文件中搜索子字符串的索引



我想在批处理文件中搜索以下示例字符串中提到的tenantDomain org_id

<ax2697:tenantDomain>org_12345678

ax2697始终是动态的,我需要检索批处理文件中的字符串org_12345678

可能需要 2 个操作。

  1. 获取"租户域>"的索引
  2. 从索引中检索org_12345678。

我在论坛上没有看到任何有希望的解决方案。
请指教。

恐怕你的问题很混乱......您需要子字符串索引还是需要检索子字符串?您会接受提取子字符串而不使用其索引的解决方案吗?:/

下面的批处理文件检索放置在较大字符串中tenantDomain>后的子字符串:

@echo off
set "string=<ax2697:tenantDomain>org_12345678"
set "x=%string:tenantDomain>=" & set "substring=%"
echo %substring%

输出:

org_12345678

试试这个:

@echo off
set "string=<ax2697:tenantDomain>org_12345678"
set "string=%string:<=#%"
set "string=%string:>=#%"
set "find_index_of=org_12345678"
call :indexof "%string%" "%find_index_of%" index
echo %index%
exit /b 0
:indexof [%1 - string ; %2 - find index of ; %3 - if defined will store the result in variable with same name]
::http://ss64.org/viewtopic.php?id=1687
@echo off
setlocal enableDelayedExpansion
set "str=%~1"
set "s=!str:%~2=&rem.!"
set s=#%s%
if "%s%" equ "#%~1" endlocal& if "%~3" neq "" (set %~3=-1&exit /b 0) else (echo -1&exit /b 0) 
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "len+=%%N"
set "s=!s:~%%N!"
)
)
endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b 0

最新更新