compact21 Posted July 11, 2015 Share Posted July 11, 2015 (edited) Hello, i have a list of sentences, each on a new line in a text file and i have this code that should find a sentence i write in the input, in that list, if not it will add it to the list. The problem is that i want it to match the full sentence only, while it matches the substrings and tells me that the sentence already exists. Could someone help me please?expandcollapse popup#include <File.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> Opt("GUIOnEventMode", 1) HotKeySet("{ENTER}", Button1Click) $Form1 = GUICreate("Quest", 425, 126, 192, 124) GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close") $OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41) GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif") GUICtrlSetColor($OkLabel, 0x00FF00) $input = GUICtrlCreateInput("", 8, 64, 369, 21) ; initialize to blank $Label2 = GUICtrlCreateLabel("Please say the quest:", 8, 24, 272, 28) GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif") $Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25) ;GUICtrlSetOnEvent($Button1, "Button1Click") $ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41) GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif") GUICtrlSetColor($ErrLabel, 0xFF0000) GUISetState(@SW_SHOW) ; read list of words to a string variable Local $sWordList = FileRead(@ScriptDir & '\sentlist.txt') While 1 Sleep(999999) ; Rip Van Winkle WEnd Func Button1Click() ; check that a word was entered if guictrlread($input) = '' then MsgBox($MB_ICONERROR, 'ERROR', "Please enter a word in the input area") GUICtrlSetState($input,$GUI_FOCUS) Return endif ; case statement to return true if word found or false if word not found Switch StringRegExp($sWordList, '(?i)'&'\b' & GUICtrlRead($input) & '\b') Case True ;MsgBox(Default, Default, "sent nOk!") GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1) Case False $sWordList &= @crlf & guictrlread($input) Filedelete(@ScriptDir & '\sentlist.txt') FileWrite(@ScriptDir & '\sentlist.txt', $sWordList) ;MsgBox(Default, Default, "sent OK!") GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1) EndSwitch GUICtrlSetData($input, "") ; blank out input control GUICtrlSetState($input,$GUI_FOCUS) ; force focus to input control EndFunc ;==>Button1Click Func Form1Close() Exit EndFunc ;==>Form1Close Edited July 11, 2015 by compact21 Link to comment Share on other sites More sharing options...
jchd Posted July 11, 2015 Share Posted July 11, 2015 Try this:; case statement to return true if word found or false if word not found If StringRegExp($sWordList, '(?im)^' & GUICtrlRead($input) & '$') Then ;MsgBox(Default, Default, "sent nOk!") GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1) Else $sWordList &= @crlf & guictrlread($input) FileWrite(@ScriptDir & '\sentlist.txt', @crlf & guictrlread($input)) ;MsgBox(Default, Default, "sent OK!") GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1) EndIf 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
compact21 Posted July 11, 2015 Author Share Posted July 11, 2015 (edited) Try this:; case statement to return true if word found or false if word not found If StringRegExp($sWordList, '(?im)^' & GUICtrlRead($input) & '$') Then ;MsgBox(Default, Default, "sent nOk!") GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1) Else $sWordList &= @crlf & guictrlread($input) FileWrite(@ScriptDir & '\sentlist.txt', @crlf & guictrlread($input)) ;MsgBox(Default, Default, "sent OK!") GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1) EndIf Thanks for your answer but now it adds the sentence to the list even if already exists, any othe ideeas? Edited July 11, 2015 by compact21 Link to comment Share on other sites More sharing options...
compact21 Posted July 11, 2015 Author Share Posted July 11, 2015 No one can help me? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 11, 2015 Moderators Share Posted July 11, 2015 compact21,Your code with jchd's amendments works fine for me. Please provide a list of inputs which result in multiple entries so that we can test.M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
compact21 Posted July 11, 2015 Author Share Posted July 11, 2015 this would be the code:expandcollapse popup#include <File.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> Opt("GUIOnEventMode", 1) HotKeySet("{ENTER}", Button1Click) $Form1 = GUICreate("Quest", 425, 126, 192, 124) GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close") $OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41) GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif") GUICtrlSetColor($OkLabel, 0x00FF00) $input = GUICtrlCreateInput("", 8, 64, 369, 21) ; initialize to blank $Label2 = GUICtrlCreateLabel("Please say the sent:", 8, 24, 272, 28) GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif") $Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25) ;GUICtrlSetOnEvent($Button1, "Button1Click") $ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41) GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif") GUICtrlSetColor($ErrLabel, 0xFF0000) GUISetState(@SW_SHOW) ; read list of words to a string variable Local $sWordList = FileRead(@ScriptDir & '\sentlist.txt') While 1 Sleep(999999) ; Rip Van Winkle WEnd Func Button1Click() ; check that a word was entered if guictrlread($input) = '' then MsgBox($MB_ICONERROR, 'ERROR', "Please enter a word in the input area") GUICtrlSetState($input,$GUI_FOCUS) Return endif ; case statement to return true if word found or false if word not found ; case statement to return true if word found or false if word not found If StringRegExp($sWordList, '(?im)^' & GUICtrlRead($input) & '$') Then ;MsgBox(Default, Default, "sent nOk!") GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1) Else $sWordList &= @crlf & guictrlread($input) FileWrite(@ScriptDir & '\sentlist.txt', @crlf & guictrlread($input)) ;MsgBox(Default, Default, "sent OK!") GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1) EndIf GUICtrlSetData($input, "") ; blank out input control GUICtrlSetState($input,$GUI_FOCUS) ; force focus to input control EndFunc ;==>Button1Click Func Form1Close() Exit EndFunc ;==>Form1Closeand the test file below sentlist.txt Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 11, 2015 Moderators Share Posted July 11, 2015 compact21,The problem is the final "?" which was threw off the RegEx as it is a "special character" inside a pattern. Try this:; case statement to return true if word found or false if word not found $stext = StringRegExpReplace(GUICtrlRead($input), "[][$^.{}()+\-?]", "\\$0") If StringRegExp($sWordList, '(?im)^' & $stext & '$') ThenNow we escape all the SRE special characters before running the RegEx comparison - that works fine for me with the phrases you were using.M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted July 11, 2015 Share Posted July 11, 2015 For a top security you could use thisIf StringRegExp($sWordList, '(?im)^\Q' & GUICtrlRead($input) & '\E$') ThenWhich does that any special character in the GuiCtrlRead will loose its special meaning and will be treated as a litteral Link to comment Share on other sites More sharing options...
jchd Posted July 11, 2015 Share Posted July 11, 2015 ? is hardly part of a "word". Anyway, here's another way:If StringRegExp($sWordList, '(?im)^\Q' & GUICtrlRead($input) & '\E$') Thenwhich should work, provided the field doesn't contain \E 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
compact21 Posted July 11, 2015 Author Share Posted July 11, 2015 it works great now, thank you all! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now