Search code examples
regexcommand-linefindstr

Command Line findstr with a regular expression


I need to search through all the files in a directory and sub directories to match any of the numbers in the reg exp. Basically in our code we have blocks of code based on certain project numbers. I need to find these blocks by project number. This regular expression does what I need but I cannot get it to work at the command line

([^0-9]|^)(56|14|2)([^0-9]|$)

I tested this on https://www.freeformatter.com/regex-tester.html against this string "If session.projid = 56 and then again 14 or something else"

I am trying this at the command line

findstr /s /R /C:"([^0-9]|^)(56|14|2)([^0-9]|$)" *.*

But no results and I know there should be. Thanks in advance for any help on this.


Solution

  • See these docs:

    FINDSTR does not support alternation with the pipe character (|) multiple Regular Expressions can be separated with spaces, just the same as separating multiple words (assuming you have not specified a literal search with /C) but this might not be useful if the regex itself contains spaces.

    In your case, you may use \< / \> word boundaries with each number and you may specify all your alternatives after a space:

    findstr /s /r "\<56\> \<14\> \<2\>" *.*