regex 批处理-使用findstr和“inverse class”-正则表达式无法正常工作

dldeef67  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(141)

我有以下问题:我尝试将带有某种“分隔符”的字符串传递给函数:即string=This.string.contains.dots.as.separators
在上面的例子中,我通过查看知道分隔符是点,但在运行时,某人也应该能够传递一个字符串,如:string=This-string-contains-hyphens-as-separators
现在我的挑战是得到分隔符。因此我尝试循环给定的字符串(如上面的两个例子)-〉char for char,如果有一个字符不在a-z**(所有字母)的**集之间,它一定是分隔符。
我尝试在findstr中找到使用“inverse class”的分隔符。有趣的是,如果我直接将findstr命令粘贴到windows cmd shell中,它将工作:例如,* 以下工作 *:
echo .|findstr "[^a-z]"
并且找到了点,如果存在除a-z以外的其他字符,则返回errorlevel=0(因此必须是分隔符)-〉recap:我遍历字符串中的char,然后在某个点上得到分隔符。
下面是我的代码片段:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:MAIN
setlocal
REM Call a function with an string and it shall return the separator in the string
call :GET_THE_SEPARATOR "This.string.is.separated.by.dots" "return_value"
echo The separator is = !return_value!
exit /b 0

:GET_THE_SEPARATOR
setlocal
set "separated_string=%~1"
REM Iterate over the first 10 chars of the string
for /l %%c in (0 1 10) do (
    set "act_char=!separated_string:~%%c,1!"
    REM Search for every char not in the alphabet (means to get i.e.: . , -)
    REM Unfortunately the findstr SYNTAX here seems to be broken, but works if you
    REM directly paste it into a console.: i.e.: echo .|findstr "[^a-z]"
    echo.!act_char! | findstr ""[^^a-z]"" >NUL && (
        set "separator=!act_char!
    )
)
REM Return the value
(endlocal 
    if "%~2" neq "" (set "%~2=%separator%")
)
exit /b 0
but5z9lq

but5z9lq1#

@ECHO OFF
SETLOCAL

SET "alphabet=a b c d e f g h i j k l m n o p q r s t u v w x y z"

CALL :getsep "This.string.is.separated.by.dots" separators
ECHO %separators% IN %originalstring%
CALL :getsep "This-string-is-separated-by-dashes" separators
ECHO %separators% IN %originalstring%
CALL :getsep "This string is separated by spaces" separators
ECHO %separators% IN %originalstring%
CALL :getsep "This*string*is*separated*by*stars" separators
ECHO %separators% IN %originalstring%
GOTO :eof

:getsep
SET "originalstring=%~1"
SET "seps=%~1"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%z IN (%alphabet%) DO SET "seps=!seps:%%z=!"
endlocal&SET "%2=%seps%"
GOTO :eof

只需将原始字符串中的每个字符替换为 nothing
如果分隔符字符是同类的,则返回字符串的第一个字符就是分隔符,否则只需删除重复的字符。

qvsjd97n

qvsjd97n2#

另一种方法可以报告多个分隔符及其在字符串中的位置

@Echo off

Set "ExampleString=test-String-example^|three^&four^>five*six^<seven=eight:nine_ten^^b~eleven!twelve!"

Setlocal EnableDelayedExpansion
Set "TestString=!%~1!"

If not defined TestString (
    Endlocal
    Call %~n0 "ExampleString"
    Goto:Eof
)

Set /A "[#]=1","Pos[#]=-1"
REM start count of character position at 0 index for potential use in substring modification
REM Set TestString

REM The below parsing method splits a string into it's compenent characters.
For /f "usebackq Delims=" %%G in (`%Systemroot%\System32\cmd.exe /u /c ^"Echo(!TestString!^"^|%Systemroot%\System32\find.exe /v "[false_match_%~n0]"^|%Systemroot%\System32\findstr.exe "^^"`)Do (
    Set Char=^^^%%~G
    For /f "UseBackQ Delims=abcdefghijklmnopqrstuvwxtzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%c in (`"Echo("!Char!""`) Do (   
        Set /A Pos[#]+=1
      If not "^%%~c" == "^^" (
            If "^^^%%~c" == "^^^!"  (
                Set Delim.![#]!.@Char.!Pos[#]!=^^^%%~c
            ) Else Set Delim.![#]!.@Char.!Pos[#]!=^%%~c
        )
        If not "^%%~c" == "^^" Set /A [#]+=1
    ) 
)

Set Delim
Pause

REM // perform any actions required with the delim information here or modify the script to return the values across the endlocal.
Endlocal & Goto:eof

相关问题