Regular expressions in findstr

100,794

Solution 1

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

Solution 2

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=

Solution 3

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

Solution 4

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]*$

Solution 5

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.

Share:
100,794
Rich
Author by

Rich

Software developer at yWorks GmbH. Usually I'll answer questions here about Java, C#, PowerShell, batch files, random numbers, regular expressions and Unicode. At work I primarily deal with C#, Java and JavaScript. My interests are mostly HCI, Interaction Design and UX. Recently I18n joined that list as well. That does not mean I have profound knowledge in either of those fields, just that I am interested in them. Note: People contacting me outside SO/SE is fine, but for discussing their own questions or answers SO already provides a mechanism, namely edits and comments. So please use those things first. I do hang around on Freenode as Hypftier, though.

Updated on June 14, 2020

Comments

  • Rich
    Rich almost 4 years

    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?

  • Rich
    Rich about 14 years
    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.
  • Rich
    Rich about 14 years
    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.
  • yoyo
    yoyo over 11 years
    I'm reluctant to up-vote such an abuse of regular expression syntax, but you solved my problem, thank-you! :-p
  • smartexpert
    smartexpert almost 7 years
    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.
  • Rich
    Rich almost 7 years
    @smartexpert: You may have noticed that Alan's answer is the accepted one.
  • Denis Howe
    Denis Howe about 6 years
    I'm fascinated to I know went anyone would want bignums in a bat file.
  • Rich
    Rich almost 5 years
    Notepad++ is not really a useful option for non-interactive scripting.