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?
I realize this is a really old post but thought it may come up in the future so I quickly hacked out a little more advanced batch solution. Normally I would just use powershell, python, ruby or vbs. It's a lot more challenging in a batch language but why not. :-P
Or if you can, download grep for windows.. Many more features than
findstr
provides.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:
This works for me:
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.)A simpler
regex
that achieves the same thing is possible, just add an optional minus to the start of your original expression: