How to use findstr regular expressions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

How to use findstr regular expressions

#1 Post by abc0502 » 22 Aug 2012 19:30

in the findstr help page, there is these commands,
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances 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


could any one put one example that show how to use them in a for command ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: can any one explain how to use one of these commands

#2 Post by foxidrive » 22 Aug 2012 20:03

Most is untested:


. Wildcard: any character

dir /b |findstr /r "a.c"
will find abc.txt or adc.txt etc

* Repeat: zero or more occurances of previous character or class

dir /b |findstr /r "a*c"
will find aaac.txt and aaaaaaac.txt etc

^ Line position: beginning of line
$ Line position: end of line


dir /b |findstr /r "^z123.txt$"
will find only z123.txt

[class] Character class: any one character in set
[^class] Inverse class: any one character not in set


dir /b |findstr /r "^a[xyz].txt$"
will find ax.txt ay.txt az.txt

dir /b |findstr /r "^a[^xyz].txt$"
will ignore ax.txt ay.txt az.txt but find aa.txt and ab.txt

[x-y] Range: any characters within the specified range

dir /b |findstr /r "^a[a-zA-Z].txt$"
will find every file starting with a and followed by any alpha character, upper or lower case, .txt EG aB.txt aZ.txt af.txt

\x Escape: literal use of metacharacter x

dir /b |findstr /r "a\[cd.txt$"
will find a[cd.txt

dir /b |findstr /r "z\$yx.txt$"

will find z$yx.txt

\<xyz Word position: beginning of word
xyz\> Word position: end of word


echo aaa bcd|findstr "\<bcd"
will find the text but
echo aaa zbcd|findstr "\<bcd"
will not find it.

echo aaa abc xyz|findstr "abc\>"
will find the text but
echo aaa abcd xyz|findstr "abc\>"
will not find it.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to use findstr regular expressions

#3 Post by abc0502 » 22 Aug 2012 20:25

Thanks, Foxidrive for your reply :D
This commands was giving me a headache, and i was manged to get only this [x-y] to work.
Your example will be very helpfull, thanks

Post Reply