提取2个字符串之间的子字符串 [英] Extract substring between 2 strings
问题描述
这是我的代码段:
set CMS_SQL="cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
set CMS_FROM_VERSION="cms913_"
set CMS_TO_VERSION="to_9133.json"
CMS_FROM_VERSION 和 CMS_TO_VERSION 均为变量
我想从%CMS_SQL%
只想获取%CMS_FROM_VERSION% and %CMS_TO_VERSION%
之间包含这两个变量值的子字符串
Just want to get the substring between %CMS_FROM_VERSION% and %CMS_TO_VERSION%
containing these two variable values
因为这两个字符串是变量,所以不希望使用file来执行此操作,也不想通过硬编码获得子字符串.
Do not want to use file to do this and also do not want to hard code number to get the substring, since the two strings are variables.
如何在这两个字符串之间提取子字符串? 谢谢
How extract substring between these two strings? Thanks
推荐答案
要再次重申我的评论,以下内容不知道或不计算子字符串的长度,它只是从字符串中删除了前两个字符和后两个字符:
To re-iterate my comment, the following does not know or count the substring length, it simply removes the first two and last two characters from the string:
Set "String=AB134SSPQS"
Set "subString=%String:~2,-2%"
Echo %subString%
如果AB
和QS
在所需的子字符串之前和之后但不一定在字符串的开头和结尾,那么这可能对您有用:
If AB
and QS
precede and succeed the required substring but do not necessarily begin and start the string, then perhaps this will work for you:
Rem Looking for a file.txt substring preceded by AB and succeeded by QS
@Echo Off
For /F "Delims=" %%A In ('FindStr /I "AB.*QS" "file.txt"') Do Call :Sub "%%A"
Pause
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Up2Sub=%Line:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
Exit /B
因为您没有提供足够的信息作为答案的依据,所以我不得不编造一个场景,(请参阅Rem
).
I've had to make up a scenario, (see Rem
), because you've not provided sufficient information on which to base an answer.
修改
Set "String=AB134SSPQS"
Set "Up2Sub=%String:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
Edit2
正如您在其他问题中给出的,并根据您现在更改的问题在此处添加,这是一种方法:
As given in your other question, and added here based on your now changed question, here's one method:
@Echo Off
Set "CMS_SQL=cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
Set "CMS_FROM_VERSION=cms913_"
Set "CMS_TO_VERSION=to_9133.json"
For /F "Delims=" %%A In ('Echo %%CMS_SQL:*%CMS_FROM_VERSION%^=%%'
) Do Set "SUBSTRING=%CMS_FROM_VERSION%%%A"
For /F "Delims=" %%A In ('Echo %%SUBSTRING:%CMS_TO_VERSION%^=^&:%%'
) Do Set "SUBSTRING=%%A%CMS_TO_VERSION%"
Set SUBSTRING
Pause
这篇关于提取2个字符串之间的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!