简体   繁体   中英

findstr Windows command regular expression

Is there a way to search for 2 or more spaces in a row between letters, using findstr from the Windows command line?

Example:

Hello world!  - nomatch wanted
Hello  world! - match wanted

What is regular expression syntax?

Also, can you please help me to understand the following command line session (difference between [ ] and [ ]* ; the second command returns nothing):

c:\1>findstr -i -r  "view[ ]*data.sub" "view data sub.acf"
View Data Sub.ACF:            "].DATE_STAMP)>=[Forms]![MainNav]![View Data Sub]"
View Data Sub.ACF:            "].DATE_STAMP)<[Forms]![MainNav]![View Data Sub]"

c:\1>findstr -i -r  "view[ ]data.sub" "view data sub.acf"

c:\1>

PS: Just curious; I know about awk, perl, C# etc., but what about findstr ?

If you just want to find two consecutive spaces:

findstr /C:"  " input.txt

Or in a case-insensitive regular expression:

findstr /R /I /C:"lo  wo" input.txt

The important bit is the /C: in front of the pattern. This tells findstr to treat the pattern as a literal string. Without it, findstr splits the pattern into multiple patterns at spaces. Which, in my experience, is never what you want.

Update

To do two or more spaces between letters:

findstr /R /I /C:"[a-z]   *[a-z]" input.txt

Note that there are three spaces in the pattern. This matches a letter, two spaces followed by zero or more spaces (ie two or more spaces) and another letter.

To find two or more consecutive spaces between letters:

C:\Users\harry> findstr /i /r /c:"o  [ ]*w" test.txt
Hello  world!
Hello   world!

Translation: match lines containing 'o', two spaces, zero or more spaces, 'w'. (The square brackets are redundant, but add clarity.) Presumably, you already know that findstr /? will give you a summary of the regular expression syntax?

As for the second part of your question: as arx already pointed out, the reason you aren't getting the results you expect is that you aren't using the /C flag. Consider your first command:

findstr -i -r  "view[ ]*data.sub" "view data sub.acf"

This is interpreted as a search for any line matching either of two regular expressions, view[ and ]*data.sub . I've done some experiments and I believe the first regex is either being discarded as improperly formed, or interpreted as requiring a match from an empty set of characters. The second regex is interpreted as follows: zero or more of ']', 'data', one arbitrary character, 'sub'. As it happens, this happens to match the same lines as the single regex you thought you were using. Not so when you take away the asterisk:

findstr -i -r  "view[ ]data.sub" "view data sub.acf"

Now the second regex is interpreted as follows: exactly one ']', 'data', one arbitrary character, 'sub'. Since the string ']data' does not occur in your text, none of the lines match. Instead, you should specify /c:

findstr /i /r /c:"view[ ]data.sub" "view data sub.acf"

Now you are searching for a single regex: 'view', a space, 'data', an arbitrary character, 'sub'. This is presumably what you wanted to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM