Page 1 of 1

How to find a string but not as a substring

Posted: 19 Nov 2010 11:31
by bill b.
Hi,
I need to find a string that 's standalone -- i.e., not embedded in another string. For example, I'd like to find "project" but not find "projectile"

Is there a way to do this? With findstr, I tried

"/<project/>" but that returned no hits.

thanks for helping!
bill

Re: How to find a string but not as a substring

Posted: 19 Nov 2010 18:47
by SenHu
This requires regular expressions. For this, you need biterscripting in Windows. Download it from any free download website. Enter this command in biterscripting.


Code: Select all

stex -r -c "^,project,^" " project projectile myproject"


You will see the results. -r means regular expression. -c means do case-insensitive search.

For regular expressions, check out http://www.biterscripting.com/helppages/RE.html .

Let's say, you want to change the free-standing word 'project' in file "/path/to/file.aspx". Here are the commands to use.

Code: Select all

var string content 
# We will read the file content into string variable $content
cat "/path/to/file.aspx" > $content
# We read the content of file into $content
sal -r -c "^,project,^" "XYZ" $content
# We replaced the free-standing word 'project' with
# XYZ.
echo $content > "/path/to/file.aspx"
# We wrote the file back with updated content.



You can put this commands in a script file C:/Replace.txt and execute this command.


Code: Select all

script "C:/Replace.txt"


It woud do the same. Obviously, change "/path/to/file.aspx" to appropriate file path and file name. Emclose it in double quotes.

If you need more help, just post. This is a great website to get windows programming questions answered.


Hope this helps.

Re: How to find a string but not as a substring

Posted: 20 Nov 2010 11:34
by !k

Code: Select all

findstr /r /c:"\<project\>" file.txt

Re: How to find a string but not as a substring

Posted: 22 Nov 2010 08:53
by bill b.
Thank you both!

!k, I tried your approach, but it didin't work. I added test cases to my file, and the syntax you describe couldn't find any strings, let alone substrings.

If I put the word "project" in an html file finstr "\<project\>" couldn't find it.

I'm running DOS on Winserver 2003. Maybe it's the version I'm running.

Again, thanks to you both. I'm going to try -r for now.

Bill
:?

Re: How to find a string but not as a substring

Posted: 22 Nov 2010 09:12
by !k
bill b.
Sample file, plz.

Re: How to find a string but not as a substring

Posted: 22 Nov 2010 14:11
by orange_batch
Is it possible to search for " project ", " project" or "project "? The space characters can provide the uniqueness required for such a search.