일괄 적으로 구분 기호를 기반으로 문자열을 하위 문자열로 분할하려고합니다.
이 예제는 작동하지만 더 나은 방법이 있습니다.
for /f "tokens=1-20 delims=:" %%a in (string.txt) do (
echo %%a& echo.%%b& echo.%%c& echo.%%d& echo.%%e& echo.%%f& echo.%%g& echo.%%h& echo.%%i& echo.%%j
echo.%%k& echo.%%l& echo.%%m& echo.%%n& echo.%%o& echo.%%p& echo.%%q& echo.%%r& echo.%%s& echo.%%t
) >substrings.txt
기본 구분 기호로 작동하지만 콜론을 사용하여 실패하는 Magoo 에서이 코드 를 시도 했습니다 .
@echo off
setLocal
for /f "delims=:" %%a in (string.txt) do (
for %%i in (%%a) do echo %%i >>substrings.txt
)
나는 또한 Aacini 에서이 코드 를 시도했지만 특수 문자를 사용하여 실패한 것 같습니다.
@echo off
setlocal EnableDelayedExpansion
set i=1
set "x=%string%"
set "x!i!=%x::=" & set /A i+=1 & set "x!i!=%"
set x >substrings.txt
% string % var 또는 string.txt의 예는 다음과 같습니다.
Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select
다음과 같이 % string % var 또는 string.txt를 substring.txt에 출력하려고합니다.
Select project
/option
report name="Sustainability"
option value="201"
Huonville
/option
/report
report name="Energy and Water Management"
option value="102"
Cygnet
/option
/report
report name="Tree Management"
option value="101"
Cygnet
/option
/report
option disabled="disabled"
/option
/select
어떤 도움이라도 대단히 감사하겠습니다. 감사
를 사용할 때 for /f
얼마나 많은 토큰이 있는지 알아야합니다 (최대 토큰에 대한 제한이 있음).
일반 for
루프 가 필요 하지만 사용할 구분 기호를 선택할 수 없습니다.
따라서 모든 구분 기호를 공백으로 바꿉니다. 원래 공간을 처리하기 위해 각 "토큰"을 따옴표로 묶어야합니다. 이렇게하려면 전체 문자열을 따옴표로 묶고 delimter :
를 " "
다음으로 바꿉니다 .
@echo off
setlocal enabledelayedexpansion
set "string=Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select""
set "string=%string: =@%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
set "out=%%~a"
echo !out:@= !
))>out.txt
echo take a look at the modified string:
echo %string%
echo/
type out.txt
다른 접근 방식 (학업 적 이유) :
@echo off
setlocal
set "string=Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select"
call :recur %string%
goto :eof
:recur
rem echo loop with %*
for /f "tokens=1,* delims=:" %%a in ("%*") do (
echo %%a
if not "%%~b" == "" call :recur %%b
goto :eof
)
echo.
참고 : 재귀 제한이 있습니다 (스택 크기로 인해). 주어진 문자열에서 작동하지만 문자열에 "토큰"이 더 포함되어 있으면 실패 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다