在Windows批处理脚本中,我需要拆分一个字符串,该字符串包含括号中的用户名和用户In,并且可能包含括号中的非雇员状态,只需要拆分为逗号分隔的用户in列表。
集INPUT=Stone,杰克(非空)(石),史密斯,约翰(史密斯),多伊米尔顿,简(多伊)
OUTPUT=stonej,smithj,doej
有人能帮忙吗?
发布于 2014-03-26 02:41:08
这解决了任务--但如果实际任务是转换文件,则会发生变化。
这应该处理普通批处理代码会遇到问题的字符。
这使用了一个名为repl.bat的助手批处理文件-从:https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat下载
将repl.bat放在与批处理文件相同的文件夹中,或放置在路径上的文件夹中。
@echo off
set "input=Stone, Jake (non-empl) (stonej), Smith, John (smithj), Doe Milton, Jane (doej)"
for /f "delims=" %%a in ('echo "%input%,"^|repl "\(non-empl\)" "" ^|repl ".*?\((.*?)\).*?," "$1," ^|repl "..$" "" ') do set output=%%a
echo "%output%"
pause发布于 2014-03-26 03:03:05
@echo off
SET INPUT=Stone, Jake (non-empl) (stonej), Smith, John (smithj), Doe Milton, Jane (doej)
for /F "tokens=4,6,8 delims=()" %%a in ("%input%") do set OUTPUT=%%a,%%b,%%c
echo OUTPUT=%OUTPUT%输出:
OUTPUT=stonej,smithj,doej发布于 2014-03-26 16:16:37
以下是本机批处理文件解决方案
@echo off
setlocal enableDelayedExpansion
set "input=Stone, Jake (non-empl) (stonej), Smith, John (smithj), Doe Milton, Jane (doej)"
:: Define LF to contain a linefeed character
set ^"LF=^
^" Above empty line is critical - DO NOT REMOVE
:: Remove (non-empl) from data
set "input=!input:(non-empl)=!"
:: insert linefeed before ( and after )
for %%L in ("!LF!") do (
set "input=!input:(=%%~L(!"
set "input=!input:)=)%%~L!"
)
:: Echo the value and pipe to FINDSTR to preserve lines containing (
:: Must delay expansion by escaping !, and use CMD /V:ON to re-enable delayed expansion
:: Use FOR /F to parse the result, discarding ( and ), and build output
for /f "delims=()" %%A in ('cmd /v:on /c echo ^^!input^^!^|findstr "("') do set output=!output!%%A,
:: Remove unwanted trailing , from output
set "output=!output:~0,-1!"
:: Show result
echo output=!output!https://stackoverflow.com/questions/22650060
复制相似问题