Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
606 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...