ur

substring search not excluding extra characters

15 posts in this topic

#1 ·  Posted (edited)

I am trying to getan exact word match in searching a substring using function StringInStr.

Like my search word is oFSO sentence may contain oFSO or OFSO= and not case-sensitive.so I created conditional syntax as

$sentence is the complete sentence and $searchword contains the searching word.

If StringInStr($sentence, " "&$searchword&" ") or StringInStr($sentence, " "&$searchword&"=")

It is working fine for most cases and it excluding words like ofs,ofs1 ,etc words

But if the word is ofso1 (i.e., contains the word we specified + extra characters) then it is not excluding that.

Could you please help how to exclude that and write condition so that it will get exact match i.e, oFSO or ofso or OFSO (not case-sensitive).

 

Edited by ur

Share this post


Link to post
Share on other sites



Why StringInStr? What about regexp?

Share this post


Link to post
Share on other sites

ur,

Please do not double-post - I have deleted the other one.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Why StringInStr? What about regexp?

I didn't tried regexp. It is bit complicated and I felt SubInStr is easy to use and the $searchword is variable not fixed to particular word.

But anything is fine. Can you tell any solution for particular context.

Edited by ur

Share this post


Link to post
Share on other sites

Sorry, the above script is working fine the problem is not with the extra characters appended.

But in my script when I need to search for exact word.As a previous solution I used
StringInStr($sentence, " "&$searchword&" ")

But I need to include words even if the sentence used $searchword and appended the word with any special characters like (=,.,& etc) but exclude if any numbers or alphabets appended to it.

Ex:    sunday12 should be excluded but sunday= or sunday& should not be excluded.

Is there any regular expression we can use in SubInStr or any other function to get this solution.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

$str = "oFSO oFSo= or mofso or oFSo& ofs,ofs1 ofso1 OFSO"
$s = "ofso"

$res = StringRegExpReplace($str, '(?i)\b' & $s & '\b[[:^alnum:]]?', "#")
msgbox(0,"", $res)

If StringRegExp($str, '(?i)\b' & $s & '\b') Then Msgbox(0,"", "match")

This expression matches any substring $s followed (or not) by a non-alphanumeric char

Edited by mikell
2nd regex fixed

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

mikell,

The part [[:^alnum:]]? is superfluous since if \b wouldn't be true if an alphanum was there after $s.

Also \b relies on the definition of \w and that includes the underscore, which may be something the OP doesn't want.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Right  :>
I placed it in the SRER to include it in the replacement, and forgot to remove it in the 2nd expression which tests the match only

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

In such case, a negative lookahead assertion is simpler and covers all bases, like $s & "(?![[:alnum:]]) but what to put inside the assertion depends on what the OP doesn't want.

Edited by jchd
Too much SQL or C implies using != too easily!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

"dfgfdg ofso" can we exclude if it is in double quotes.I mean if the string itself is having double quote characters and it is inside them.

Edited by ur

Share this post


Link to post
Share on other sites

Can yo explain this regular expression and can you provide any link where there is details about the pattern creation in regular expression.

Share this post


Link to post
Share on other sites

My task is I am looking for a particular set of words by reading it from a file.The complete file is loaded to a variable $strScript.

Now I am searching with the expression provided above 

$res = StringRegExpReplace($str, '(?i)\b' & $s & '\b[[:^alnum:]]?', "#")

But we need to exclude the word if it is in the double quotes.

Ex: I am searching for sunday.

And if the file contains 10 lines and if the word we found at line 8 then we also need to check whether the word is in a double quotes.
"One of the holiday is sunday for this week.".

If it is double quotes then we need to exclude that.

Share this post


Link to post
Share on other sites

This should give you the idea:

$str = '"One of the holiday is sunday for this week." ... and today is not sunDay (but I love Sundays even if today is not sunday!)'
$s = "SuNDay"

ConsoleWrite($str & @LF)
$res = StringRegExpReplace($str, '".*?"', "")   ; removes anything inside pair of double quotes
ConsoleWrite($res & @LF)

$res = StringRegExpReplace($res, '(?i)\b' & $s & '(?![[:alnum:]])', '###')  ; replaces $s by ###
ConsoleWrite($res & @LF)

You can use this site to play with Perl (PCRE) regexp.

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Nice site jchd, I guess there is no "find next" though?  couldn't find it.

I have been using Notepad++ it has regex option for searching but I like how this site helps tutor with the meaning of each part of your RegEx maybe this is what I needed to finally learn some of this stuff. 

Share this post


Link to post
Share on other sites

I don't get the "find next" part. I don't know which regexp flavor Motepad++ uses and which precise possibilities it offers.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a log file with several entries like the following one:
      INFO [26.04.2017 11:37:48] [main] XML-Data: <online-activation> <general> <userid>XYZ</userid> <mac-address/> <OU>VG-DE</OU> <ROLE>KDT</ROLE> <FOA>PRO;FC;DOM;MDD</FOA> <BRD>XYZ;IMP</BRD> </general> <applications> <app expiration-date="2017-10-01" name="BB-INFO"/> <app expiration-date="2017-10-01" name="MMSKD"/> <app expiration-date="2017-10-01" name="FM-TOOL"/> <app expiration-date="2017-04-05" name="WEB-BTD"/> <app expiration-date="2017-10-01" name="OFFLINE-BTD"/> <app expiration-date="2017-10-01" name="MDU-UPD"/> <app expiration-date="2017-10-01" name="MDU"/> <app expiration-date="2017-04-05" name="WEB-WDA"/> <app expiration-date="2017-04-05" name="WEB-ETD"/> <app expiration-date="2017-10-01" name="OFFLINE-WDA"/> <app expiration-date="2017-10-01" name="OFFLINE-ETD"/> </applications> </online-activation>  
      I need to collect all the XML-Data, my search doesn't work:
       
      Func FileSearch() $j=0 For $i = 0 To UBound($content_array) - 1 $search_result=StringInStr($content_array[$i],$search) If $search_result<>0 Then ReDim $searchResultArray[UBound($searchResultArray) + 1] $searchResultArray [$j] = $content_array[$i] _ArrayDisplay($searchResultArray) $j+=1 EndIf Next EndFunc If i search for "online-activation", it retrieves only the first and the last tag, but not what is within.
      In addition, when i use FileReadToArray, all the entries between <online-activation> and </online-activation>

    • MrSparty1995
      By MrSparty1995
      I have a good handle on how to read a file line by line, and search for a given string.
      Basically using, FileOpen, FileReadLine, and StringInStr.
      I have been trying to figure out why my search keeps coming back with no match.
      For example, if my string line was "Where is Waldy", and I was searching for Waldy that comes back fine.
      But in my file, Let's say it is
      Where is "Waldy"
      So i read that line in and do a search for Waldy, but i comes back with no match since there is quotes around it.
      Is there a way to resolve this?
      Here is my actual example
      The line comes back like this
          <JposEntry logicalName="LineDisp_iSC480">
      I'm searching for LineDisp_iSC480, but its coming back with no match due to the quotes.
      Thanks in advance, James
    • AliOzturk
      By AliOzturk
      Hello, I want to be able to search for multiple strings and if any of the strings are found I want it returned which one it is. I've been able to search for multiple strings using the OR command, but I'm not sure how I am supposed to make it return which of my strings is found. I've given an example below
      If StringInStr($html, Batman) OR StringInStr($html, Superman) Then MsgBox(0, "Success", "The string found was: Superman") EndIf So it will search for the two strings and whichever it founds it will return me with. How can I make that work?
    • rootx
      By rootx
      #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> Local $path="C:\Users\rootx\Desktop\Nuova cartella",$mask = "*.jpg;*.png;*.ico",$Pos $FileArr = _FileListToArrayRec($path,$mask,1,1,1,2) For $x = 1 to UBound($FileArr)-1 If $Pos = StringInStr($FileArr[$x],"-") Then ConsoleWrite("------"&$FileArr[$x]&@CRLF) Else ConsoleWrite($FileArr[$x]&@CRLF) FileDelete($FileArr[$x]) EndIf ConsoleWrite($x&@CRLF) Next ConsoleWrite($x-1&@CRLF) I would write a loop that individuals and delete files that contain this feature....
      File name-150x100.jpg, or other -SizexSize
      But in this case how can I filter properly?
      File name 150x100-150x100.jpg
      File name -100-150x100.jpg
       
    • jrmm
      By jrmm
      I love Autoit, but struggling to find the best way to do this.
      I have 2 text files....  File1 is Tab Separated and File2 is CSV.  I am attempting to read in a line from File1 and split it, then search the contents of File2 for one of the substrings from that split line.    If I find a match, then I want to split the matching line and grab needed info from it to complete the record and output the combined record to a file.
       
      Example line from File1:
          17   123456789     learned          10800     bridging      1/13
      Example line from File2:
      172.25.17.103,computername.domain,Reservation (active),DHCP,123456789,,Full Access,N/A
      From File1, I grab the 2nd column (MAC Address) and search for that in the 2nd file (Column5)
       
      I really thought reading file into an array and doing an _arraysearch was the way to go, but that seems to require searching for the whole string, not a sub-string.
      What I have been trying to do so far is read in file1 one line at a time, split the string, then read in file 2 one line at a time and check for a matching substring..... handle that.   Then back to file 1 to repeat the process.   This seems so clunky.
      So I would love suggestions on what functions might work best.
      Thanks so much for any feedback.
      - John