I have a following requirement that needs to be achieved in .bat file. Can some one please help.
There is a string, ABCD-1234 TEST SENTENCE
in a variable, say str. Now I want to check if the string starts with format [A-Z]*-[0-9] *
or not.
How can I achieve this? I tried various regular expression using FINDSTR
, but couldn't get the desired result.
Example:
set str=ABCD-1234 TEST SENTENCE
echo %str% | findstr /r "^[A-Z]*-[0-9] *"
I'm assuming you are looking for strings that begin with 1 or more upper case letters, followed by a dash, followed by 1 or more digits, followed by a space.
If the string might contain poison characters like &
, <
, >
etc., then you really should use delayed expansion.
FINDSTR regex is totally non-standard. For example, [A-Z]
does not properly represent uppercase letters to FINDSTR, it also includes most of the lowercase letters, as well as some non-English characters. You must explicitly list all uppercase letters. The same is true for the numbers.
A space is interpreted as a search string delimiter unless the /C:"search"
option is used.
setlocal enableDelayedExpansion
echo(!str!|findstr /rc:"^[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]*-[0123456789][0123456789]* "
You should have a look at What are the undocumented features and limitations of the Windows FINDSTR command?