如果找到特殊文本,我需要从字符串中取出ECHO文本。我需要ECHO的文字是()。
在下面的代码片段中,我需要在()中搜索“language =”。如果找到“language =”,我需要将()内的文本回显到控制台。
使用STRING1 ECHO语言= en应该打印。
使用STRING2时,不应该回应任何内容。
非常感谢任何帮助。
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
SET STRING1=What A Nice Morning (language=en). More text.
SET STRING2=What A Nice Morning. More text.
CALL :FETCHLANGUAGE "%STRING1%"
CALL :FETCHLANGUAGE "%STRING2%"
PAUSE
:FETCHLANGUAGE
ECHO Parse String %1
ECHO "%1" | FINDSTR /C:"language=" 1>nul
IF ERRORLEVEL 1 (
ECHO Language not found
) ELSE (
ECHO Found Language
REM *** Need to ECHO language=xyz here.
)
EXIT /B
答案 0 :(得分:4)
如果您的问题是关于检索子字符串,那么这可能会有所帮助。
set var=%var:*(lang=%
这将删除(lang
第一次出现的所有字符。
要删除剩余的一行,可能还有另一种技巧。
set var=%var:)=&rem #%
它在右括号
答案 1 :(得分:2)
尝试使用for / f
for /f "tokens=2 delims=()" %%L in ('ECHO "%1" ^| FINDSTR "language="') do (
echo %%L
)
答案 2 :(得分:2)
如果您下载gnu grep for Windows,则解决方案很简单。
我同时使用正向前瞻和正向外观来寻找但排除封闭的()
:
@echo off
setlocal enableDelayedExpansion
set string1=What a nice morning (language=en). More text.
set string2=What a nice morning. More text.
echo !string1!|grep -o -P "(?<=\()language=.*?(?=\))"
echo !string2!|grep -o -P "(?<=\()language=.*?(?=\))"
exit /b
有些人处于不允许或无法下载和安装可执行文件的情况。我写了一个正则表达式搜索和替换名为REPL.BAT的脚本非常方便。它作为混合批处理/ JScript脚本实现。它应该适用于任何现代Windows平台(XP及更高版本)。
这是一个使用我的REPL.BAT的解决方案(假设REPL.BAT位于PATH中或者当前文件夹中)此解决方案在任何匹配的语言文本之后不会打印换行符。我使用s
选项允许REPL.BAT直接从环境变量中读取值。
@echo off
setlocal
set string1=What a nice morning (language=en). More text.
set string2=What a nice morning. More text.
call repl "(.*?(\((language=.*?)\)))?.*" "$3" s string1
call repl "(.*?(\((language=.*?)\)))?.*" "$3" s string2
在任何匹配的语言文字后,稍有变化会打印换行符。我添加x
选项以在替换文本中启用转义序列。即使没有语言文本的行在输出中获得换行符,因此需要将输出传递给FINDSTR以消除空行。
@echo off
setlocal
set string1=What a nice morning (language=en). More text.
set string2=What a nice morning. More text.
repl "(.*?(\((language=.*?)\)))?.*" "$3\r\n" xs string1 | findstr .
repl "(.*?(\((language=.*?)\)))?.*" "$3\r\n" xs string2 | findstr .
这是REPL.BAT脚本,可以使上述功能正常工作。完整文档嵌入在脚本中。它非常强大和高效 - 我已经成功地使用它来解决许多搜索和替换批处理脚本问题。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::************ Documentation ***********
:::
:::REPL Search Replace [Options [SourceVar]]
:::REPL /?
:::
::: Performs a global search and replace operation on each line of input from
::: stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: If called with a single argument of /? then prints help documentation
::: to stdout.
:::
::: Search - By default this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript syntax documentation is available at
::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
::: Replace - By default this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substituion patterns available to the JScript replace method.
::: A $ literal can be escaped as $$. An empty replacement string
::: must be represented as "".
:::
::: Replace substitution pattern syntax is documented at
::: http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
::: Options - An optional string of characters used to alter the behavior
::: of REPL. The option characters are case insensitive, and may
::: appear in any order.
:::
::: I - Makes the search case-insensitive.
:::
::: L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in Replace are
::: treated as $ literals.
:::
::: E - Search and Replace represent the name of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: M - Multi-line mode. The entire contents of stdin is read and
::: processed in one pass instead of line by line. ^ anchors
::: the beginning of a line and $ anchors the end of a line.
:::
::: X - Enables extended substitution pattern syntax with support
::: for the following escape sequences:
:::
::: \\ - Backslash
::: \b - Backspace
::: \f - Formfeed
::: \n - Newline
::: \r - Carriage Return
::: \t - Horizontal Tab
::: \v - Vertical Tab
::: \xnn - Ascii (Latin 1) character expressed as 2 hex digits
::: \unnnn - Unicode character expressed as 4 hex digits
:::
::: Escape sequences are supported even when the L option is used.
:::
::: S - The source is read from an environment variable instead of
::: from stdin. The name of the source environment variable is
::: specified in the next argument after the option string.
:::
::************ Batch portion ***********
@echo off
if .%2 equ . (
if "%~1" equ "/?" (
findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" ""
exit /b 0
) else (
call :err "Insufficient arguments"
exit /b 1
)
)
echo(%~3|findstr /i "[^SMILEX]" >nul && (
call :err "Invalid option(s)"
exit /b 1
)
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b
************* JScript portion **********/
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) {
options+=args.Item(2).toLowerCase();
}
var multi=(options.indexOf("m")>=0);
var srcVar=(options.indexOf("s")>=0);
if (srcVar) {
options=options.replace(/s/g,"");
}
if (options.indexOf("e")>=0) {
options=options.replace(/e/g,"");
search=env(search);
replace=env(replace);
}
if (options.indexOf("l")>=0) {
options=options.replace(/l/g,"");
search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");
replace=replace.replace(/\$/g,"$$$$");
}
if (options.indexOf("x")>=0) {
options=options.replace(/x/g,"");
replace=replace.replace(/\\\\/g,"\\B");
replace=replace.replace(/\\b/g,"\b");
replace=replace.replace(/\\f/g,"\f");
replace=replace.replace(/\\n/g,"\n");
replace=replace.replace(/\\r/g,"\r");
replace=replace.replace(/\\t/g,"\t");
replace=replace.replace(/\\v/g,"\v");
replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g,
function($0,$1,$2){
return String.fromCharCode(parseInt("0x"+$0.substring(2)));
}
);
replace=replace.replace(/\\B/g,"\\");
}
var search=new RegExp(search,options);
if (srcVar) {
WScript.Stdout.Write(env(args.Item(3)).replace(search,replace));
} else {
while (!WScript.StdIn.AtEndOfStream) {
if (multi) {
WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace));
} else {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace));
}
}
}