批处理脚本 - 由两个或多个空格分隔

时间:2022-09-23 00:09:14

I have a large string that looks like this

我有一个大字符串,看起来像这样

aa bb c d  f eeeee    ffff

I am not sure of the number of spaces that would come along with the string. The next time, string might have five spaces between aa and bb ansd so on...

我不确定字符串会带来多少空格。下一次,字符串可能在aa和bb之间有五个空格,所以......

aa     bb     c      f       eee ff

Is there a way I can split on two or more spaces (variable number of spaces) using delims in batch script?I always want the second token no matter how any spaces are there between first and second tokens.

有没有办法可以在批处理脚本中使用delim在两个或多个空格(可变数量的空格)上拆分?我总是想要第二个令牌,无论第一个和第二个令牌之间有多少空格。

PS : Edit I want the second token (by token I mean the token obtained after splitting by exactly two or more spaces). My token itself can contain a single space. Example: a b c d. In this want to extract b. Another example: a b b c d . In this I want to extract b b.

PS:编辑我想要第二个标记(通过标记我指的是在分割恰好两个或更多个空格后获得的标记)。我的令牌本身可以包含一个空格。示例:a b c d。在此想要提取b。另一个例子:a b b c d。在这里我想提取b b。

2 个解决方案

#1


1  

(Collected from various comments: Tokens are delimited by two or more spaces; You need the second token; Every token may or may not have single spaces)

(从各种评论中收集:标记由两个或多个空格分隔;您需要第二个标记;每个标记可能有也可能没有单个空格)

I commented every step and added an echo to show progress.

我评论了每一步,并添加了一个回声来显示进度。

@echo off
setlocal 
set "string=ads ads      d b c    dsad   ds   ads"
echo 1: "%string%"
REM remove first token (delimited by ecactly two spaces):
set "string=%string:*  =%"
echo 2: "%string%"
REM remove leading spaces:
for /f "tokens=*" %%a in ("%string%") do set "string=%%a"
echo 3: "%string%"
REM remove third and next tokens (delimited by two or more spaces)
set string=%string:  =&REM %
echo 4: "%string%"

Only change to my answer to your previous question is the for to remove leading spaces

只更改为我之前问题的答案是删除前导空格

#2


0  

Notice for future readers:

关于未来读者的通知:

This answer was posted before OP added the significant text I always want the second token no matter how any spaces are there between first and second tokens.

这个答案是在OP添加重要文本之前发布的,我总是想要第二个令牌,无论第一个和第二个令牌之间有多少空格。

Please read the original question before voting/commenting

请在投票/评论前阅读原始问题


No.

delims means that tokens are separated by any sequence of any of the defined delimiter characters.

delims表示令牌由任何已定义的分隔符字符的任何序列分隔。

Hence your example string would be processed as 7 separate tokens.

因此,您的示例字符串将被处理为7个单独的标记。

If you were to tell us what you are attempting to do,it may help. The approach taken often depends on the desired results.

如果您要告诉我们您尝试做什么,可能会有所帮助。采取的方法通常取决于期望的结果。

Probably this is associated with https://stackoverflow.com/a/48808982/2128947 to which you have yet to respond.

这可能与您尚未回复的https://stackoverflow.com/a/48808982/2128947相关联。

#1


1  

(Collected from various comments: Tokens are delimited by two or more spaces; You need the second token; Every token may or may not have single spaces)

(从各种评论中收集:标记由两个或多个空格分隔;您需要第二个标记;每个标记可能有也可能没有单个空格)

I commented every step and added an echo to show progress.

我评论了每一步,并添加了一个回声来显示进度。

@echo off
setlocal 
set "string=ads ads      d b c    dsad   ds   ads"
echo 1: "%string%"
REM remove first token (delimited by ecactly two spaces):
set "string=%string:*  =%"
echo 2: "%string%"
REM remove leading spaces:
for /f "tokens=*" %%a in ("%string%") do set "string=%%a"
echo 3: "%string%"
REM remove third and next tokens (delimited by two or more spaces)
set string=%string:  =&REM %
echo 4: "%string%"

Only change to my answer to your previous question is the for to remove leading spaces

只更改为我之前问题的答案是删除前导空格

#2


0  

Notice for future readers:

关于未来读者的通知:

This answer was posted before OP added the significant text I always want the second token no matter how any spaces are there between first and second tokens.

这个答案是在OP添加重要文本之前发布的,我总是想要第二个令牌,无论第一个和第二个令牌之间有多少空格。

Please read the original question before voting/commenting

请在投票/评论前阅读原始问题


No.

delims means that tokens are separated by any sequence of any of the defined delimiter characters.

delims表示令牌由任何已定义的分隔符字符的任何序列分隔。

Hence your example string would be processed as 7 separate tokens.

因此,您的示例字符串将被处理为7个单独的标记。

If you were to tell us what you are attempting to do,it may help. The approach taken often depends on the desired results.

如果您要告诉我们您尝试做什么,可能会有所帮助。采取的方法通常取决于期望的结果。

Probably this is associated with https://stackoverflow.com/a/48808982/2128947 to which you have yet to respond.

这可能与您尚未回复的https://stackoverflow.com/a/48808982/2128947相关联。