0

I need to ECHO text out of a string if a special text is found. The text I need to ECHO is between ().

In the following code fragment I need to search for "language=" within (). If "language=" is found I need to ECHO the text that's within () to the console.

With STRING1 ECHO language=en should be printed.

With STRING2 nothing should be echoed.

Any help is highly appreciated.

@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
jeb
  • 78,592
  • 17
  • 171
  • 225
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85

3 Answers3

4

If your question is about retrieving a substring, then this could help.

set var=%var:*(lang=% This removes all charcters to the first occurence of (lang.

To remove the rest of a line, there is another trick possible. set var=%var:)=&rem #%
It injects a REM command at the position of the closing bracket

jeb
  • 78,592
  • 17
  • 171
  • 225
  • +1, thank you for this. Do you have a link to more about such code injection? I think, this doesn't work with DelayedExpansion. – Endoro Apr 05 '13 at 13:40
  • Thanks - but this doesn't work here. I did use that exactly, only replaced the %var by %1, and echo %var is empty - always. – Harald Wilhelm Apr 05 '13 at 14:18
  • @HaraldWilhelm: You cannot apply this kind of processing to positional parameters in batch script. You'll need to store the entire `%1` into a variable first, then manipulate the variable using the method in this answer. – Andriy M Apr 05 '13 at 15:20
  • Ah, I see. I went with the other answer. Thanks anyway. – Harald Wilhelm Apr 05 '13 at 15:33
  • @Endoro You are right, it can't work with delayed expansion. [SO:CMD nested double quotes...](http://stackoverflow.com/a/12553559/463115) or [SO: Is this batch file injection?](http://stackoverflow.com/questions/8254286/is-this-batch-file-injection) – jeb Apr 05 '13 at 16:18
  • +1, It is possible to use these techniques to build a fairly robust solution. But there is still the possibility of failure because of inability to match `=`, and also inability to use delayed expansion when executing the `&REM #` injected text. This comment is more for readers, since I know jeb is already aware of the limitations. – dbenham Apr 06 '13 at 22:14
2

try with for /f

for /f "tokens=2 delims=()" %%L in ('ECHO "%1" ^| FINDSTR "language="') do (
  echo %%L
) 
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    Thanks - but this returned a syntax error: "('ECHO". I had to replace your code with "for /f "tokens=2 delims=()" %%L in (%1) do (echo %%L). That worked. Thanks. – Harald Wilhelm Apr 05 '13 at 14:32
  • Yeah. I've forgotten the `in`.Also the escaping the double quotes is not necessary .Answer is edited now.Have on mind that `=` is a delimiter in batch and if they are nor in quotes `language=(en)` will be two parameters. – npocmaka Apr 05 '13 at 16:09
  • This solution will fail under many circumstances. For example: `early (parens) fails (language=en)`, as does `wrong language=trouble (fails)` – dbenham Apr 06 '13 at 21:34
2

The solution is trivial if you download gnu grep for Windows.

I use both positive lookahead and positive lookbehind to look for, yet exclude, the enclosing ():

@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

Some people are in a situation where they are not allowed or able to download and install an executable. I have written a regex search and replace script called REPL.BAT that is extremely handy. It is implemented as a hybrid batch/JScript script. It should work on any modern Windows platform (XP and beyond).

Here is a solution using my REPL.BAT (assuming REPL.BAT is either in the PATH or else in the current folder) This solution does not print a newline after any matching language text. I use the s option to allow REPL.BAT to read the value directly from the environment variable.

@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

A slight variation prints a newline after any matching language text. I add the x option to enable the use of escape sequences in the replacement text. Even lines that don't have the language text get a newline in the output, hence the need to pipe the output to FINDSTR to eliminate empty lines.

@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 .

Here is the REPL.BAT script that enables the above to work. Full documentation is embedded within the script. It is surprisingly powerful and efficient - I have successfully used it to solve many search and replace batch scripting problems.

@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));
    }
  }
}
dbenham
  • 127,446
  • 28
  • 251
  • 390