42

I'm doing a little string validation with findstr and its /r flag to allow for regular expressions. In particular I'd like to validate integers.

The regex

^[0-9][0-9]*$

worked fine for non-negative numbers but since I now support negative numbers as well I tried

^([1-9][0-9]*|0|-[1-9][0-9]*)$

for either positive or negative integers or zero.

The regex works fine theoretically. I tested it in PowerShell and it matches what I want. However, with

findstr /r /c:"^([1-9][0-9]*|0|-[1-9][0-9]*)$"

it doesn't.

While I know that findstr doesn't have the most advanced regex support (even below Notepad++ which is probably quite an achievement), I would have expected such simple expressions to work.

Any ideas what I'm doing wrong here?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Note to the editor: The *windows* tag is a little superfluous, given that `findstr` only exists there ... – Joey Apr 10 '10 at 15:41
  • 17
    Rössel: Tags are there to help find things. A moderate redundancy doesn't hurt in this case. – jfs Apr 10 '10 at 20:18
  • I'd never search for a tag as overcrowded as *windows* ;-) but if you think it'll help ... – Joey Apr 10 '10 at 22:44
  • 12
    and don't forget the tag usage to filter out questions. Those overcrowded tags as *windows*, *linux*, *apple*, are **very** convenient to *ignore* – PA. Mar 15 '11 at 13:21

5 Answers5

62

This works for me:

findstr /r "^[1-9][0-9]*$ ^-[1-9][0-9]*$ ^0$"

If you don't use the /c option, the <Strings> argument is treated as a space-separated list of search strings, which makes the space a sort of crude replacement for the | construct. (As long as your regexes don't contain spaces, that is.)

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • Aah, right you are. That one I forgot. Yes, spaces in the RE would be bad, then but that won't be an issue here. – Joey Apr 11 '10 at 08:53
  • 3
    I'm reluctant to up-vote such an abuse of regular expression syntax, but you solved my problem, thank-you! :-p – yoyo Dec 12 '12 at 02:32
9

Argh, I should have read the documentation better. findstr apparently doesn't support alternations (|).

So I'm probably back to multiple invocations or replacing the whole thing with a custom parser eventually.

This is what I do for now:

set ERROR=1
rem Test for zero
echo %1|findstr /r /c:"^0$">nul 2>&1
if not errorlevel 1 set ERROR=
rem Test for positive numbers
echo %1|findstr /r /c:"^[1-9][0-9]*$">nul 2>&1
if not errorlevel 1 set ERROR=
rem Test for negative numbers
echo %1|findstr /r /c:"^-[1-9][0-9]*$">nul 2>&1
if not errorlevel 1 set ERROR=
Joey
  • 344,408
  • 85
  • 689
  • 683
  • As suggested by Alan Moore, if your search string does not contain spaces, then the space character will work like | in 'regular' regular expressions :) if all you're doing is looking to output any line that contains a list of strings you have. – smartexpert Jul 27 '17 at 15:37
  • @smartexpert: You may have noticed that Alan's answer is the accepted one. – Joey Jul 27 '17 at 15:54
3

Or if you can, download grep for windows.. Many more features than findstr provides.

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • No option here. This is a pure batch bignum library. I'll go for a proper parser when I'm done with the basic arithmetic. This will also be much better for proper error messages. For now multiple `findstr` invocations should suffice. Also, if it were just for features I'd just call PowerShell. Much easier, much more powerful. – Joey Apr 10 '10 at 15:22
  • 1
    I'm fascinated to I know went anyone would want bignums in a bat file. – Denis Howe Mar 10 '18 at 10:54
1

A simpler regex that achieves the same thing is possible, just add an optional minus to the start of your original expression:

^-?[0-9][0-9]*$
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
martski
  • 342
  • 1
  • 5
  • This is incorrect: it will match negative zero, and integers with a leading zero, either positive or negative: `-0`, `01`, `-042` etc. while OP's regex does not. Also, `findstr` does not support the `?` quantifier, so this answer really is not relevant to OP's issue – joH1 Feb 10 '23 at 17:31
0

Support for regex in findstr is quite limited. I suggest using Notepad++. The find in files option supports Perl Compatible Regular Expressions; results showing filename, line number and matching text can be easily copied to a text file.

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72