AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1512500
Accepted
tar
tar
Asked: 2019-12-26 11:36:21 +0800 CST2019-12-26 11:36:21 +0800 CST 2019-12-26 11:36:21 +0800 CST

命令行:获取 2 个不同分隔符之间的子字符串

  • 772

我如何能够在两个不同的定界符之间获得一个子字符串,这两个定界符也位于给定字符串中的可变位置。

例如:

String1 = "my first example {{my first substring}}"
String2 = "my great second example which has much more words included before {{my second substring}} and after the substring"
Substring1 = "my first substring"
Substring2 = "my second substring"

如您所见,子字符串之前的分隔符是“{{”,之后的分隔符是“}}”。

我发现的关于子字符串操作的所有内容都是严格的位置设置,在这里没有帮助。

提前致谢!

command-line batch-file
  • 3 3 个回答
  • 625 Views

3 个回答

  • Voted
  1. Akina
    2019-12-26T12:18:22+08:002019-12-26T12:18:22+08:00
    @echo off
    cls
    setlocal ENABLEDELAYEDEXPANSION
    
    set String1=my first example {{my first substring}}
    set String2=my great second example which has much more words included before {{my second substring}} and after the substring
    
    call :extract %String1% 
    echo %Substring%
    call :extract %String2% 
    echo %Substring%
    
    endlocal
    goto :EOF
    
    :extract
    for /f "delims={ tokens=2" %%x in ("%*") do (
        set Substring=%%x
    )
    for /f "delims=}" %%x in ("%Substring%") do (
        set Substring=%%x
    )
    exit /b
    

    我不相信这会给出正确的结果。例如 --- {{abc {123} xyz}} --- 产生 abc 但我认为正确的结果是 abc {123} xyz。– 德本汉姆

    这是正确的。为了解决这个问题,我们必须有一些源字符串中不存在的可打印字符(至少在最终定界符之前)。

    例如,如果这样的 char 是@代码可能是

    @echo off
    cls
    setlocal ENABLEDELAYEDEXPANSION
    
    set String1=my first example {{my first substring}}
    set String2=my great second example which has much more words included before {{my second substring}} and after the substring
    set String3=--- {{abc {123} xyz}} ---
    
    call :extract %String1% 
    echo %Substring%
    call :extract %String2% 
    echo %Substring%
    call :extract %String3% 
    echo %Substring%
    
    endlocal
    goto :EOF
    
    :extract
    set tempstr=%*
    set tempstr=%tempstr:{{=@%
    set tempstr=%tempstr:}}=@%
    for /f "delims=@ tokens=2" %%x in ("%tempstr%") do (
        set Substring=%%x
    )
    exit /b
    
    • 1
  2. tar
    2019-12-26T12:27:58+08:002019-12-26T12:27:58+08:00

    我想我找到了解决方案:

    set string="my great second example which has much more words included before {{my second substring}} and after the substring"
    echo string: %string%
    set "tmp=%string:{=" & set "tmp=%"
    echo tmp:    %tmp%
    for /f "delims=}" %%a in ("%tmp%") do set substr=%%a
    echo substr: %substr%
    

    结果:

    string: "my great second example which has much more words included before {{my second substring}} and after the substring"
    tmp:    "my great second example which has much more words included before "
    substr: my second substring
    

    它有效,但我不太明白它如何能够确定 tmp-string 的子字符串,因为它不在其内容中。

    • 0
  3. Best Answer
    dbenham
    2019-12-27T14:46:40+08:002019-12-27T14:46:40+08:00

    我假设您只需要每个字符串中的一个子字符串。这是一个强大的解决方案,它可以处理任何值,但它不能在值中处理,如果!在值中,也会^被剥离!。

    @echo off
    setlocal enableDelayedExpansion
    for %%S in (
      "my {{first substring}} example"
      "my {ignore {{second substring}} example"
      "my {{third {sub}string}} example"
      "poison character {{&|<>^^ "^&^|^<^>^^^^"}} example"
      "empty {{}} example"
      "my {first error}} example"
      "my {{second error} example"
      "my third error example{{"
      ""
      "Limitation: {{eclamation is stripped^^^!}}"
      "Limitation: {{caret ^^^^ is stripped if exclamation present^^^!}}"
    ) do (
      set "string=%%~S"
      echo(!string!
      call :get{{sub}} string sub && echo(!sub! || echo ERROR: substring not found
      echo(
    )
    exit /b
    
    :get{{sub}}  inStrVar  subStrVar
    setlocal enableDelayedExpansion
    set "str=!%~1!"
    if not defined str exit /b 1
    set "sub1=!str:*{{=!"
    if not defined sub1 exit /b 1
    if "!sub1!"=="!str!" exit /b 1
    set "sub="
    for %%N in (^"^
    
    ^") do for /f "delims=" %%A in ("x!sub1:}}=%%~N!") do if not defined sub set "sub=%%A"
    set "sub=!sub:~1!"
    if "!sub!"=="!sub1!" exit /b 1
    for /f delims^=^ eol^= %%A in (""!sub!"") do endlocal & set "%~2=%%~A" & exit /b 0
    

    - 输出 -

    my {{first substring}} example
    first substring 
    
    my {ignore {{second substring}} example
    second substring 
    
    my {{third {sub}string}} example
    third {sub}string 
    
    poison character {{&|<>^ "&|<>^"}} example
    &|<>^ "&|<>^" 
    
    empty {{}} example
    
    
    my {first error}} example
    ERROR: substring not found
    
    my {{second error} example
    ERROR: substring not found
    
    my third error example{{
    ERROR: substring not found
    
    
    ERROR: substring not found
    
    Limitation: {{eclamation is stripped!}}
    eclamation is stripped 
    
    Limitation: {{caret ^ is stripped if exclamation present!}}
    caret  is stripped if exclamation present 
    

    获取子字符串有两个主要技巧。

    • !str:*{{!通过第一次出现 . 删除所有内容{{。
    • for %%N in ...在 in 创建一个带引号的换行符,%%N以便!sub:{{=%%~N!用换行符替换 all }}。内部for /f然后分别迭代每个字符串,do循环只保留第一个结果。x以防万一子字符串为空。
    • 0

相关问题

  • 如何制作批处理脚本来备份具有唯一目标文件夹的特定文件(Windows)[关闭]

  • 如何在 macOS 的终端中切换切换(连续性)?

  • Windows 上是否有可以通过 CMD 或 Powershell 提供加密安全随机字节的设备或可执行文件?

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve