0

After searching for few days, all I get is that how to check a variable is it in alphabets or numeric form. Is there any way to combine both condition together?

1st condition: 1st letter must be "D"

2nd condition: after "D" must be numeric

Example of variable: D123456789

Thanks for viewing, comments and answers.

2 Answers2

1

The following should work as long as the variable does not contain any "poison" characters like &, |, etc.

echo(%var%|findstr /rx "D[0123456789]*" >nul && (
  echo Valid
) || (
  echo Invalid
)

If you want to protect against poison characters, then

setlocal enableDelayedExpansion
echo(!var!|findstr /rx "D[0123456789]*" >nul && (
  echo Valid
) || (
  echo Invalid
)

If the last command that you execute in the "success" ('&&') section might raise an error, then the ERRORLEVEL must be cleared, otherwise the "failure" ('||') section might fire when you don't want. The simplest and fastest way is to use (call )

setlocal enableDelayedExpansion
echo(!var!|findstr /rx "D[0123456789]*" >nul && (
  echo Valid
  someCommandThatCouldRaiseAnError
  (call )
) || (
  echo Invalid
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Dave, you can also quote the filename/variable and use `"^.D[0123456789]*.$"` as the search term but `^` and `$` are optional. – foxidrive Mar 31 '14 at 14:29
  • @foxidrive - quoting the value using normal expansion might not be safe. It could fail if there are both quotes and poison characters in the value. – dbenham Mar 31 '14 at 15:13
  • You're right - if the string can be any character. Will it also fail with Unicode characters or characters outside the code page? – foxidrive Mar 31 '14 at 15:34
  • @foxidrive - I believe my posted code with delayed expansion will always work. – dbenham Mar 31 '14 at 15:47
  • Btw.. is the answer lack of 1 closing bracket ")" or extra 1 open bracket "(" ? Or I am wrong. – Thor_that_new_in_programming Apr 01 '14 at 00:43
  • The `ECHO(` is a valid hack that allows printing of lines with nothing but spaces without getting the `ECHO is off.` (or on) message. It should not have a closing parenthesis. A more common form is `ECHO.`, but that form can fail under certain obscure scenarios. `ECHO(` is the only form that always works. – dbenham Apr 01 '14 at 00:48
  • Woah, thanks for the explanation. Now I learned another new thing. Thanks! – Thor_that_new_in_programming Apr 01 '14 at 00:51
0

This looks like a proper use case for a regular expression, something such as /^D[0-9]+/.

FindStr might help you doing Regexpes in Batch scripts: you can check out this manual: http://www.robvanderwoude.com/findstr.php

user3387633
  • 121
  • 11
  • The FINDSTR regex support is rudimentary at best, and very non-standard - it does not support your suggested regex. The documentation you cite is mostly parroting MS built in help, which is woefully inadequate. The claim that FINDSTR cannot search across line breaks is wrong. Best to read [What are the undocumented features and limitations of the Windows FINDSTR command?](http://stackoverflow.com/q/8844868/1012053). – dbenham Mar 31 '14 at 11:36