Search code examples
regexfindstr

CMD Findstr - Line starting with 13 digits


I have multiple lines like this

1480438326593   addons.xpi-utils    DEBUG   shutdown

and I want to parse them with FINDSTR function from windows CMD.

My problem now is the arguments don't work, or probably I am doing it wrong, but it should work.

I am using this command findstr /V /R ^\d{13} which should use regex and find any digit 13 times at the start of a string.

findstr /V /R ^\d this does work as intended if it starts with a digit but the {13} doesn't work - any help?


Solution

  • To return lines that start with 13 digits use

    findstr /r ^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
    

    If you want to fail (not match) cases where the 13 digits are followed with more digits (i.e. do not match 12345678901234 more text lines), add \> (trailing word boundary) at the end.

    The findstr utility does not support proper regex, only some wildcard patterns, so, there is no limiting quantifier (i.e. {min,max}) support, nor shorthand character classes like \d.

    Here is the table of patterns findstr supports:

    ┌───────────┬─────────────────────────────────────────────────────────────────┐
    │ Character │ Value                                                           │
    ├───────────┼─────────────────────────────────────────────────────────────────┤
    │    .      │ Wildcard: any character                                         │
    │    *      │ Repeat: zero or more occurrences of previous character or class │
    │    ^      │ Line position: beginning of line                                │
    │    $      │ Line position: end of line                                      │
    │ [class]   │ Character class: any one character in set                       │
    │ [^class]  │ Inverse class: any one character not in set                     │
    │   [x-y]   │ Range: any characters within the specified range                │
    │   \x      │ Escape: literal use of metacharacter x                          │
    │  \<xyz    │ Word position: beginning of word                                │
    │   xyz\>   │ Word position: end of word                                      │
    └───────────┴─────────────────────────────────────────────────────────────────┘
    

    Note that adding \v option will reverse the results: you will get all lines that do not start with 13 digits.