findstr in dos - regular expressions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dalizardking427
Posts: 2
Joined: 18 Apr 2013 20:40

findstr in dos - regular expressions

#1 Post by dalizardking427 » 18 Apr 2013 20:46

How can i get a range of 3 to 5 lowercase letters using findstr with regular expressions.
For example:
abc
defg
aqwer
Any of the above should match. SO far the only way i know to do it is like this:
findstr /rc:"[a-z][a-z][a-z][a-z][a-z]" .\*
findstr /rc:"[a-z][a-z][a-z][a-z]" .\*
findstr /rc:"[a-z][a-z][a-z]" .\*

is there a way to do it in one command?

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

Re: findstr in dos - regular expressions

#2 Post by foxidrive » 18 Apr 2013 21:43

This is one way, but there is a bug with multiple strings and and the /i switch should be used too.

findstr /r /c:"[a-z][a-z][a-z][a-z][a-z]" /c:"[a-z][a-z][a-z][a-z]" /c:"[a-z][a-z][a-z]" .\*


A problem with your implementation is that this will match any number of lowercase letter strings over 2 characters

findstr /r /c:"[a-z][a-z][a-z]" .\*


You need to limit the regexp by the characteristics of the input text.

Reply with a sample of the text and what you want, if you would like some ideas.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: findstr in dos - regular expressions

#3 Post by dbenham » 18 Apr 2013 22:15

Sorry, there is not a good solution. :cry: :evil: FINDSTR has very primitive (lousy) regex support.

What you posted does not even work because FINDSTR has a screwy implementation of regex ranges: [a-z] doesn't only match lower case letters - it also matches A-Y, as well as some non-English characters.

The only way to match lower case English characters with FINDSTR is to use [abcdefghijklmnopqrstuvwxyz].

The only repeat specification that FINDSTR supports is *, which matches zero or more times.

The maximum regex search string length on Vista and beyond is 254 characters. The best you can do on modern platforms is this god-awful search (note the use of line continuation):

Code: Select all

findstr /r /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]" ^
  /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]" ^
  /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]"

On XP, the maximum regex search string length is only 127 characters. So it is absolutely impossible to use FINDSTR to match 3-5 lower case letters on XP :(

For a complete description of the crappy FINDSTR range implementation - see Why does findstr not handle case properly (in some circumstances)?

For an exhaustive listing of FINDSTR quirks, see What are the undocumented features and limitations of the Windows FINDSTR command?

--------------

foxidrive wrote:but there is a bug with multiple strings and and the /i switch should be used too.
The bug is restricted to multiple string literal searches. The /I switch is not needed when using multiple regex searches.


Dave Benham

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

Re: findstr in dos - regular expressions

#4 Post by foxidrive » 18 Apr 2013 23:12

dbenham wrote:The maximum regex search string length on Vista and beyond is 254 characters. The best you can do on modern platforms is this god-awful search (note the use of line continuation):

Code: Select all

findstr /r /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]" ^
  /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]" ^
  /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]"

On XP, the maximum regex search string length is only 127 characters. So it is absolutely impossible to use FINDSTR to match 3-5 lower case letters on XP :(


But this will match any matching string for 3 or more characters. Still not what the OP wants.

findstr /r /c:"[abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz]" e.txt

The bug is restricted to multiple string literal searches. The /I switch is not needed when using multiple regex searches.


Thanks.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: findstr in dos - regular expressions

#5 Post by dbenham » 19 Apr 2013 04:36

foxidrive wrote:But this will match any matching string for 3 or more characters. Still not what the OP wants.

It will match any line that contains 3 to 5 consecutive lower case letters anywhere within the string, which is what I thought the OP asked for. But now that you question it, I realize it is a silly requirement. Simply asking for a line that contains 3 consecutive lower case letters would give the same result.

But put add some additional terms to the beginning and end of the search strings, and then it becomes useful (ignoring the fact that the syntax is totally impractical).

If the intent is to match lines that consist solely of 3-5 lower case letters, then simply add the /X option.


Dave Benham
Last edited by dbenham on 19 Apr 2013 14:22, edited 1 time in total.

dalizardking427
Posts: 2
Joined: 18 Apr 2013 20:40

Re: findstr in dos - regular expressions

#6 Post by dalizardking427 » 19 Apr 2013 08:03

I found the way!!!!

findstr /s /r "\<[a-z][a-z][a-z][a-z][a-z]\> \<[a-z][a-z][a-z][a-z]\> \<[a-z][a-z][a-z]\> " .\*

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: findstr in dos - regular expressions

#7 Post by dbenham » 19 Apr 2013 08:24

No, that will also match upper case letters A through Y. Did you even read my posts :?: :?

FINDSTR is not a good option for your stated requirements.

You should download and use a version of grep for Windows. The gnu Win32 project has a good free version of grep for Windows: http://gnuwin32.sourceforge.net/packages/grep.htm


Dave Benham

Post Reply