0

I want to extract a sub-string based on pattern from a given string in batch script.

Example:

Input :  ABCDEF-x32-32.12.20-298-date-20-12-17.exe
Required Output : 32.12.20-298  (i.e. <number>.<number>.<number>-<number>)
Regex (may be)  : [0-9]+\.[0-9]+\.[0-9]+-[0-9]+

How do I implement this in batch script??? I tried to implement the same using 'For' and 'findstr', but failed to do so.

user
  • 383
  • 1
  • 5
  • 20
  • will the substring you're looking for always be surrounded with hyphens `-` ? – J.Baoby Feb 01 '17 at 14:32
  • It may or may not. – user Feb 01 '17 at 15:10
  • [`findstr`](http://ss64.com/nt/findstr.html) searches for the sub-string, but it returns every *line* that contains a match; furthermore, `findstr` does not support the meta-character`+`, so `[0-9]+` must be written as `[0-9][0-9]*`. So basically, the wanted string is enclosed within non-numeric characters? – aschipfl Feb 01 '17 at 15:26
  • @aschipfl you may assume that. But the enclosing non-numeric characters on both side of wanted string may not be same. – user Feb 01 '17 at 15:41
  • Also consider using `powershell` if you can, its [support of regex](https://technet.microsoft.com/en-us/library/2007.11.powershell.aspx) is much more modern. – Aaron Feb 01 '17 at 15:49
  • Sorry, PowerShell is not in the options. – user Feb 01 '17 at 15:51
  • @MayankAgrawal I'm the one who feel sorry for you then, good luck with `cmd`'s 80s syntax ;) – Aaron Feb 01 '17 at 17:05
  • 1
    Possible duplicate of [How to match IP address by using 'findstr'? Or any other method of batch in windows](http://stackoverflow.com/questions/9994395/how-to-match-ip-address-by-using-findstr-or-any-other-method-of-batch-in-wind) – JosefZ Feb 01 '17 at 23:42
  • It is not a duplicate. Answer to that post simply tells about how to match the pattern whereas this post is about how to extract the pattern from the given input. – user Feb 02 '17 at 02:47

1 Answers1

2

Windows batch does not have a good regular expression tool. The FINDSTR command has very limited (non-standard, and bugged) regex support. And it only can return entire lines that contain a match.

There are native Windows scripting languages that do have good support. PowerShell certainly does, And so do JScript and VBScript (available via CSCRIPT).

If you really want a pure script based regex solution from within a batch file, then you can use JREPL.BAT. It is hybrid batch/JScript that conveniently brings the power of JScript regular expressions to the batch world.

Full documentation is available via jrepl /?, or jrepl /?? for paged help. Another important help command is jrepl /?options to get a summary of all available options, and jrepl /?help to get a summary of all available help.

Normally JREPL is used to perform a find/replace operation. But the /MATCH option ignores the required replace string and simply outputs the matching text.

Normally JREPL reads from stdin or a file. But the /S option reads the input from a variable.

The output is typically stdout or a file. But the /RTN option saves the result to an environment variable (entire result must fit within ~8kb).

@echo off
setlocal
set "str=ABCDEF-x32-32.12.20-298-date-20-12-17.exe"

:: Extract the string and print to the screen
call jrepl "[0-9]+\.[0-9]+\.[0-9]+-[0-9]+" "" /match /s str

:: Extract the string and store it in variable out.
call jrepl "[0-9]+\.[0-9]+\.[0-9]+-[0-9]+" "" /match /s str /rtn out
dbenham
  • 127,446
  • 28
  • 251
  • 390