I want to search all files in a certain directory for occurrences of statements such as
Load frmXYZ
I am on Windows 7, using the findstr
command. I tried:
findstr /n Load.*frm *.*
But this gives me unwanted results such as:
If ABCFormLoaded Then Unload frmPQR
So I tried to put a blankspace between Load
and frm
and gave the command like this:
findstr /n Load frm *.*
But this simply searched for all occurrences of the word load
or all occurrences of the word frm
. How do I get around this problem?
Use word delimiter regex
I used the the special
\<
"beginning of word" regex symbol.I tried this on the Win10 version of findstr. But according to Microsoft this special
\<
symbol has been infindstr.exe
ever since WinXP.Full (and painful) breakdown of many options that do NOT work below.
At the very bottom: what actually worked.
The sample file itself
Wrong. With regular execution space is treated as delimiter.
Wrong: With Regex option space is STILL treated as delimiter.
More right but still wrong. With /C option we now get preserved spaces but don't find other character cases.
Wrong. /I for "Ignore Case" does not help. We get matches from within words we did not want.
Right. Use special "Beginning of word" regex symbol. Matches beginning-of-line or space.
Either case sensitive:
or ignoring case
Use the
/c
option:From the help (
findstr /?
):If you use spaces, you need the
/C:
option to pass the the literal string(s) to the regex/R
option.Once the it gets to the regex, it's treated as a regex.
That said, this is typical MS trash.
Use two regex search strings
The bottom line is that you have to use 2 strings to handle cases where
Load frm
is at the beginning like so:Load frm apples bananas carrots
OR in the middle like so:
some other text Load frm and more
.Version without character classes
Below is using XP sp3, windows 7 may be different, both are trash!
findstr /N /R /C:" *Load *frm" /C:"^Load *frm" test.txt
Mind the Colon
NOTE: The colon in
/C:
is MANDATORY for this to work.If you leave out the colon then
findstr
's error handling is just to treat/C
as an invalid option, ignore that invalid option and go ahead anyway. Leading to unexpected and unwanted output.Equivalent version using character classes
findstr /N /R /C:"[ ][ ]*Load[ ][ ]*frm" /C:"^Load[ ][ ]*frm" test.txt
Character classes breakdown
A real regex might be
\bLoad\s+frm
This piece of code will only allow letters, numbers, underscore and white space in keyword: