且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

提取2个字符串之间的子字符串

更新时间:2023-02-12 18:30:40

要再次重申我的评论,以下内容不知道或不计算子字符串的长度,它只是从字符串中删除了前两个字符和后两个字符:

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%

如果ABQS在所需的子字符串之前和之后但不一定在字符串的开头和结尾,那么这可能对您有用:

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