Jump to content
compact21

StringRegExp full string match

Recommended Posts

compact21

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?

#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 by compact21

Share this post


Link to post
Share on other sites
jchd

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 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
compact21

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 by compact21

Share this post


Link to post
Share on other sites
compact21

No one can help me?

Share this post


Link to post
Share on other sites
Melba23

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

 


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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
compact21

this would be the code:

#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   ;==>Form1Close

and the test file below

sentlist.txt

Share this post


Link to post
Share on other sites
Melba23

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 & '$') Then

Now we escape all the SRE special characters before running the RegEx comparison - that works fine for me with the phrases you were using.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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
mikell

For a top security you could use this

If StringRegExp($sWordList, '(?im)^\Q' & GUICtrlRead($input) & '\E$') Then

Which does that any special character in the GuiCtrlRead will loose its special meaning and will be treated as a litteral

Share this post


Link to post
Share on other sites
jchd

? is hardly part of a "word". Anyway, here's another way:

If StringRegExp($sWordList, '(?im)^\Q' & GUICtrlRead($input) & '\E$') Then

which 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 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
compact21

it works great now, thank you all!

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

×