" delims =#+#" - 更多然后1个字符作为分隔符 [英] "delims=#+#" - more then 1 character as delimiter

查看:272
本文介绍了" delims =#+#" - 更多然后1个字符作为分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能确定哪些不限定于1个字符的定界符?基于标题的例子,我想我的定义作为分隔符例如
#+#。 TEXTFILES /线路可同时包含字符,但很少有机会,你会遇到那个特定子/文组合。

Is it possible to define a delimiter which is not limited to 1 character? Based on the title's example, I would like to define my separator as e.g. '#+#'. Textfiles/lines can contain both characters, but there is very little chance you'll come across that particular substring/text combo.

推荐答案

没有,你不能使用的字符串的作为一个分隔符 delims = 条款。当然,你可以包含字符串,但它将作为一组将​​被用作分隔符,而不是作为一个分隔符字符串的字符分开来处理。

No, you can not use a string as a delimiter in the delims= clause. Of course you can include the string, but it will be handled as a set of separate characters that will be used as delimiters, not as a delimiter string.

如果你真的需要分割的字符串,最快的办法是更换的分隔字符串的一个字符不包含在数据,并使用该字符作为分隔符

If you really need to split on a string, the fastest approach could be to replace the delimiter string by a character not included in the data and use this character as delimiter

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "delims=" %%a in ("this is a +test!! #+# of string #splitting#") do (
        set "buffer=%%a"
        setlocal enabledelayedexpansion
        (for /f "tokens=1,2 delims=¬" %%b in ("!buffer:#+#=¬!") do (
            endlocal
            echo full line    : [%%a]
            echo first token  : [%%b]
            echo second token : [%%c]
        )) || if "!!"=="" endlocal 
    )

注意:在 SETLOCAL enabledelayedexpansion 是需要能够读取里面的循环检索数据(直接在这里模拟了包括字符串)。然后,循环tokenizes的readed线内,推迟扩张禁用,以避免与字符的问题(如果延迟扩展是积极的,他们会被解析器消耗)。这是原因 ENDLOCAL 内循环。

Note: The setlocal enabledelayedexpansion is needed to be able to read the variable changed inside the for loop retrieving the data (here simulated directly including a string). Then, inside the for loop that tokenizes the readed line, delayed expansion is disabled to avoid problems with the ! characters (if delayed expansion is active, they will be consumed by the parser). This is the reason for the endlocal inside the loop.

由于我们正在做一个字符串替换,它有可能与一个只有分隔符组成的字符串结束,它可能是内部<$ C的不要条款$ C>为将不会被执行,因此最终如果包含以保证 enabledelayedexpansion 将被取消。

As we are doing a string replacement and it is possible to end with a string composed of only delimiters, it is possible that the do clause of the inner for will not be executed, so the final if is included to ensure that the enabledelayedexpansion is cancelled.

这篇关于&QUOT; delims =#+#&QUOT; - 更多然后1个字符作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆