Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

exact string search


  • Please log in to reply
5 replies to this topic
twhyman
  • Members
  • 348 posts
  • Last active: Sep 17 2014 01:55 AM
  • Joined: 07 Dec 2005
Hi,

iam new to AHK and had a problem couple days ago to find cmd.exe
in a text file, and the only way to find cmd.exe and only this, this is the code that worked for me, the code was given to me by one of the members:
{
FileRead, File, 2.txt
Found := False  ;assume not found 
Loop, Parse, File, `n 
  { 
    Pos :=  InStr(A_LoopField,"azureus.exe") 
    If Pos 
      { 
         StringMid, Char , A_LoopField , % Pos - 1, 1 
         If Char is not alnum 
            { 
             Found := True 
             Break 
           } 
       } 
  } 

i think it would be great to make a command like "exactsearch" to look for and exact string not use this much code

thanks

Chris
  • Administrators
  • 10727 posts
  • Last active: Sep 14 2014 06:10 PM
  • Joined: 02 Mar 2004
I could use clarification on what is meant by "exact search". The script provided doesn't communicate much to me, but I only studied it a few minutes.

twhyman
  • Members
  • 348 posts
  • Last active: Sep 17 2014 01:55 AM
  • Joined: 07 Dec 2005
thanks for the quick reply :)

when i load a txt file to memory as a variable and i do :
IfInString, loadedfile, cmd.exe
that code will find me cmd.exe and totalcmd.exe

i need a command that will look for the exact string i entered,
the code above does just that , and its a lot of code for a "simple" task

i a noob at AHK and it was very hard for me to get this "exact search" going , and more experienced guys then me had to write lots of code.

as a noob i tought that if i write the file name and not using any cmd.* for wildcard for example will just find me the file quick and easy, but i was wrong :D

and i was think that a command that will do that will graet for noobs like me :)

Thanks

Chris
  • Administrators
  • 10727 posts
  • Last active: Sep 14 2014 06:10 PM
  • Joined: 02 Mar 2004
Thanks for clarifying. Although clearly useful, I don't think this will become a built-in feature due to rarity of use.

However, someone could write an includable function for distribution with the program. In addition, there is a RegEx library available that I think can do searches like this in a single line. However, RegEx has a learning curve if you're completely unfamiliar with it.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
The problem seems to be that you want to find a string, delimited on either end by non-alphanumeric keys. Until AHK has regular expression search, you have to use some code. What you listed is not perfect, because it will find cmd.exec when you search for cmd.exe, and if a line contains a wrong match, a possible good match afterward is not found, like in "MyCMD.exe is not cmd.exe".The following seems to be better.
Text = MyCMD.exe is not cmd.exe

SearchText = cmd.exe



Loop {

   StringGetPos Pos, Text, %SearchText%,, % Pos+1

   IfEqual Pos,-1, Break

   StringMid Char, Text, %Pos%, 1

   If Pos <> 0

      If Char is alnum

         Continue

   StringMid Char, Text, % Pos+StrLen(SearchText)+1, 1

   If Char <>

      If Char is alnum

         Continue

   Break

}

MsgBox %Pos%   ; position of the first match, -1: not found


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Yes, it seems that twhyman wants something like the "word match" option of some text editors (found also in Wordpad).

Laszlo's code is even more "lot of code", but it can be put in a function and reused in a simple way.

Coding this would be impractical in AHK, as the meaning of "word" is somewhat fuzzy. Some text editors allows to define a set of alphanumeric chars defining such a word, allowing double-click selection and find option.
Depending on context, one would want to allow _ - ' or even é ç or @ in the words... Or course, Chris can also add a way to define the char set of word.

As Chris points out, the use case is quite rare, it may not worth to put it in AHK. Regular expressions would be more powerful, but as they would be used only by power users, perhaps the current solution (an external DLL) is probably practical enough (except you have to distribute the DLL with your scripts...).

If twhyman wants to play with PCREAHK, I would suggest to try the "[^[:alnum:]]yourword[^[:alnum:]]" expression.

OK, I finally made the real test, as it allows me to test this PCRE stuff. The expression wasn't so simplistic, as we need to match the search string at the beginning or the end of the line:
text = cmd.exec and MyCMD.exe are not cmd.exe
searchText = cmd.exe
pattern := "(?:[^[:alnum:]]|^)" . searchText . "(?:[^[:alnum:]]|$)"
res := RE_Match(text, 0, pattern, 1, result)
If (res == 0)
{
	MsgBox %pattern% Not found
}
Else If (res < 0)
{
	MsgBox Error (%res%): %result%
}
Else
{
	StringSplit, x, result, %A_Space%
	StringMid found, text, x1 + 1, x2
	MsgBox Result of RE_Match: %result%, found: %found%
}
RE wonderful and powerful :-)
I use the RE_Match function given in the MATCH.AHK script.

For those wondering and not willing to read the whole specification, the (?:foo|bar) syntax allows to search for either foo or bar without capturing the string. Above, 'foo' is clear (any non-alphanumeric char), 'bar' is either the start (^) or end ($) of the string.