Sign in to follow this  
Followers 0
magician13134

SRE matching first/last letter?

10 posts in this topic

#1 ·  Posted (edited)

I have a list of several words in a text file, and I need to match words based on the first and last letter. So let's say that I know the first letter is "h" and the last letter is "o", I would like this to find the world "hello" if it is on the list. I read the tutorial in the help file and the thirty minute tutorial linked to in that, and I still am unsure, thanks.

This is what I have, but it's not working...

$answers = StringRegExp($contents, "\b" & $letters[0][1] & ".*" & $letters[1][1] & "\b", 1)
;$letters[0][1] = first letter
;$letters[1][1] = second letter
Edited by magician13134

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$answers = StringRegExp($contents, "\b" & $letters[0][1] & "\B*" & $letters[1][1] & "\b", 1)

Maybe try that? \B means "not at word boundary."

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Ooh, right. That makes sense. Let me try that.

Hmmm... it's still not working. Maybe having the whole source code would help. It's a game called SmartMouth that I'm try to write. The idea is two letters are generated, and you must find words that start and end with those. No proper nouns, that's why it's case sensitive.

Here it is:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $alphabet1[26]
Dim $alphabet2[26]
Dim $letters[2][2]

$alphabet1[0]="a"   ;   A
$alphabet1[1]="b"   ;   B
$alphabet1[2]="c"   ;   C
$alphabet1[3]="d"   ;   D
$alphabet1[4]="e"   ;   E
$alphabet1[5]="f"   ;   F
$alphabet1[6]="g"   ;   G
$alphabet1[7]="h"   ;   H
$alphabet1[8]="i"   ;   I
$alphabet1[9]="j"   ;   J
$alphabet1[10]="k"  ;   K
$alphabet1[11]="l"  ;   L
$alphabet1[12]="m"  ;   M
$alphabet1[13]="n"  ;   N
$alphabet1[14]="o"  ;   O
$alphabet1[15]="p"  ;   P
$alphabet1[16]="t"  ;   Q
$alphabet1[17]="r"  ;   R
$alphabet1[18]="s"  ;   S
$alphabet1[19]="t"  ;   T
$alphabet1[20]="u"  ;   U
$alphabet1[21]="v"  ;   V
$alphabet1[22]="w"  ;   W
$alphabet1[23]="s"  ;   X
$alphabet1[24]="y"  ;   Y
$alphabet1[25]="z"  ;   Z

$alphabet2[0]="a"   ;   A
$alphabet2[1]="b"   ;   B
$alphabet2[2]="c"   ;   C
$alphabet2[3]="d"   ;   D
$alphabet2[4]="e"   ;   E
$alphabet2[5]="f"   ;   F
$alphabet2[6]="g"   ;   G
$alphabet2[7]="h"   ;   H
$alphabet2[8]="i"   ;   I
$alphabet2[9]="e"   ;   J
$alphabet2[10]="k"  ;   K
$alphabet2[11]="l"  ;   L
$alphabet2[12]="m"  ;   M
$alphabet2[13]="n"  ;   N
$alphabet2[14]="o"  ;   O
$alphabet2[15]="p"  ;   P
$alphabet2[16]="t"  ;   Q
$alphabet2[17]="r"  ;   R
$alphabet2[18]="s"  ;   S
$alphabet2[19]="t"  ;   T
$alphabet2[20]="u"  ;   U
$alphabet2[21]="v"  ;   V
$alphabet2[22]="w"  ;   W
$alphabet2[23]="s"  ;   X
$alphabet2[24]="y"  ;   Y
$alphabet2[25]="s"  ;   Z

$GUI = GUICreate("Smart Mouth", 311, 420, 193, 115)
$startButton = GUICtrlCreateButton("Generate", 20, 200, 100, 35)
$showButton = GUICtrlCreateButton("Show answers", 180, 200, 100, 35)
$letters[0][0] = GUICtrlCreateLabel("", 30, 50, 80, 125)
GUICtrlSetFont(-1, 40, 10000)
GUICtrlSetBkColor(-1, 0x33FF11)
GUICtrlSetColor(-1, 0x000000)
$letters[1][0] = GUICtrlCreateLabel("", 190, 50, 80, 125)
GUICtrlSetFont(-1, 40, 10000)
GUICtrlSetBkColor(-1, 0xEE8800)
GUICtrlSetColor(-1, 0x000000)
$answersEdit = GUICtrlCreateInput("", 24, 256, 265, 150)
GUISetState(@SW_SHOW)
$file = FileOpen("words.txt", "r")
$contents = FileRead($file)

While 1
    $msg = GUIGetMsg()
    If $msg == $GUI_EVENT_CLOSE Then quit()
    If $msg == $startButton Then getLetters()
    If $msg == $showButton Then getAnswers()
WEnd
    
Func getLetters()
    $letters[0][1] = $alphabet1[Random(0, 25, 1)]
    $letters[1][1] = $alphabet2[Random(0, 25, 1)]
    GUICtrlSetData($answersEdit, "")
    GUICtrlSetData($letters[0][0], " " & StringUpper($letters[0][1]))
    GUICtrlSetData($letters[1][0], " " & StringUpper($letters[1][1]))
EndFunc

Func getAnswers()
    ConsoleWrite($letters[0][1] & @CRLF & $letters[1][1] & @CRLF)
    $data = ""
    $answers = StringRegExp($contents, "\b" & $letters[0][1] & "\B*" & $letters[1][1] & "\b", 1)
    If not IsArray($answers) Then
        Return False
    EndIf
    For $i = 0 to Ubound($answers)
        $data&=$answers[$i]
    Next
    GUICtrlSetData($answersEdit, $data)
EndFunc

Func quit()
    FileClose($file)
    Exit
EndFuncoÝ÷ Øý½æ¥²kçm+ºÚ"µÍÌÍØ[ÝÙÈHÔÝ[Ð]ÙY[   ÌÍØÛÛ[Ë   [È ÌÍÛ]ÖÌVÌWK    ÌÍÛ]ÖÌWVÌWH   [ÈÔJ

The word list can be downloaded in this file:

http://hood.sjfn.nb.ca/~eddie/wscr.html
Make sure you rename it to .txt Edited by magician13134

Share this post


Link to post
Share on other sites

First off, there are three better ways to populate an array of characters, you should probably use the last one since it doesn't store the element count:

Dim $alphabet1[26]
;Dim $alphabet2[26]
Dim $letters[2][2]

;Populate array with characters from a-z
$iIndex = 0
For $X = Asc("a") to Asc("z")
    $alphabet1[$iIndex] = Chr($X)
    $iIndex += 1
Next

;Populate array with characters from a-z (number of elements in first index)
$alphabet1 = StringSplit("abcdefghijklmnopqrstuvwxyz","")

;Populate array with characters from a-z
$alphabet1 = StringRegExp("abcdefghijklmnopqrstuvwxyz",".",3)

For $X = 0 to 25
    ConsoleWrite($alphabet1[$X] & @CRLF)
NextoÝ÷ Ø    ݱç(Ùr¶z±~ò¢êàzÐ'³«±û§rب«­¢+ØÀÌØíÉÉäôMÑÉ¥¹IáÀ ÀÌØí½¹Ñ¹ÑÌ°ÅÕ½ÐìÀäÈí ÅÕ½ÐìµÀìÀÌØí±ÑÑÉÍlÁulÅtµÀìÅÕ½Ð츨ÅÕ½ÐìµÀìÀÌØí±ÑÑÉÍlÁulÅtµÀìÅÕ½Ðì¤ÀäÈíÅÕ½Ðì°Ì

Share this post


Link to post
Share on other sites

Well, the reason I wasn't doing this:

For $X = Asc("a") to Asc("z")

$alphabet1[$iIndex] = Chr($X)

$iIndex += 1

Next

Is because it's not the complete alphabet, there's some missing, but yes, that third method is much simpler. I forgot about that.

And the regular expression is still a little messed up. It returns words, but a lot of them and it seems in just one array value...

Share this post


Link to post
Share on other sites

Try replacing the .* with [a-z]* using the rest of weaponx's code. .* will keep searching past the ends of words into other words.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Try replacing the .* with [a-z]* using the rest of weaponx's code. .* will keep searching past the ends of words into other words.

Right, I had already replaced that with \B* like "Richard Robertson" suggested, they both return the same thing. One array.... thing... I can't believe I can't think of the word for that... one array value..? containing every word starting with the first letter... I'm pretty puzzled by that...

EDIT

Actually, you guys were right (that sounds rude, I said actually because I was mistaken) I had $Letters[0][1] twice instead of having $letters[0][1] and $letters[1][1], then I guess I have an input control instead of an edit accounting for the one-linedness, rather than one array value. Let me fix those things and get back to you.

Yup, that was the problem/those were the problems. Thanks guys and sorry for being careless

Edited by magician13134

Share this post


Link to post
Share on other sites

You referred to me as if I'm two different people?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ha ha... totally missed that. A little awkward...

Anyway guys, here it is, finished(ish), there's one bug, it doesn't say "None Found" when no words are found, which is a bit odd... But whatever. Not necessary.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>

Dim $alphabet1[26]
Dim $alphabet2[26]
Dim $letters[2][2]
Dim $pScore[8]
Dim $pAdd1[8]
Dim $pAdd2[8]
Dim $pSub1[8]
Dim $score[8]
For $i = 0 to 5
    $score[$i] = 0
Next

$alphabet1[0]="a"   ;   A
$alphabet1[1]="b"   ;   B
$alphabet1[2]="c"   ;   C
$alphabet1[3]="d"   ;   D
$alphabet1[4]="e"   ;   E
$alphabet1[5]="f"   ;   F
$alphabet1[6]="g"   ;   G
$alphabet1[7]="h"   ;   H
$alphabet1[8]="i"   ;   I
$alphabet1[9]="j"   ;   J
$alphabet1[10]="k"  ;   K
$alphabet1[11]="l"  ;   L
$alphabet1[12]="m"  ;   M
$alphabet1[13]="n"  ;   N
$alphabet1[14]="o"  ;   O
$alphabet1[15]="p"  ;   P
$alphabet1[16]="t"  ;   Q
$alphabet1[17]="r"  ;   R
$alphabet1[18]="s"  ;   S
$alphabet1[19]="t"  ;   T
$alphabet1[20]="u"  ;   U
$alphabet1[21]="v"  ;   V
$alphabet1[22]="w"  ;   W
$alphabet1[23]="s"  ;   X
$alphabet1[24]="y"  ;   Y
$alphabet1[25]="z"  ;   Z

$alphabet2[0]="a"   ;   A
$alphabet2[1]="b"   ;   B
$alphabet2[2]="c"   ;   C
$alphabet2[3]="d"   ;   D
$alphabet2[4]="e"   ;   E
$alphabet2[5]="f"   ;   F
$alphabet2[6]="g"   ;   G
$alphabet2[7]="h"   ;   H
$alphabet2[8]="i"   ;   I
$alphabet2[9]="e"   ;   J
$alphabet2[10]="k"  ;   K
$alphabet2[11]="l"  ;   L
$alphabet2[12]="m"  ;   M
$alphabet2[13]="n"  ;   N
$alphabet2[14]="o"  ;   O
$alphabet2[15]="p"  ;   P
$alphabet2[16]="t"  ;   Q
$alphabet2[17]="r"  ;   R
$alphabet2[18]="s"  ;   S
$alphabet2[19]="t"  ;   T
$alphabet2[20]="u"  ;   U
$alphabet2[21]="v"  ;   V
$alphabet2[22]="w"  ;   W
$alphabet2[23]="s"  ;   X
$alphabet2[24]="y"  ;   Y
$alphabet2[25]="s"  ;   Z

$GUI = GUICreate("Smart Mouth", 411, 420)
$startButton = GUICtrlCreateButton("Generate", 20, 162, 100, 35)
$showButton = GUICtrlCreateButton("Show answers", 180, 162, 100, 35)
$letters[0][0] = GUICtrlCreateLabel("", 30, 10, 80, 125)
GUICtrlSetFont(-1, 40, 10000)
GUICtrlSetBkColor(-1, 0x33FF11)
GUICtrlSetColor(-1, 0x000000)
$changeLetter1 = GUICtrlCreateInput("", 30, 139, 15, 20)
    GUICtrlSetLimit(-1, 1)
$doChange1 = GUICtrlCreateButton("Change", 50, 139, 60, 20)
$changeLetter2 = GUICtrlCreateInput("", 190, 139, 15, 20)
    GUICtrlSetLimit(-1, 1)
$doChange2 = GUICtrlCreateButton("Change", 210, 139, 60, 20)
$letters[1][0] = GUICtrlCreateLabel("", 190, 10, 80, 125)
GUICtrlSetFont(-1, 40, 10000)
GUICtrlSetBkColor(-1, 0xEE8800)
GUICtrlSetColor(-1, 0x000000)
$loading = GUICtrlCreateLabel("", 24, 200, 250)
$answersEdit = GUICtrlCreateEdit("", 20, 215, 260, 150)
GUICtrlCreateLabel("Check is a word is valid:", 20, 370, 150, 17)
$checkWord = GUICtrlCreateInput("", 20, 385, 150, 20)
$checkbutton = GUICtrlCreateButton("Check", 175, 385, 40, 20)
$valid = GUICtrlCreateLabel("", 225, 377, 75, 50)
    GUICtrlSetFont(-1, 21, 2000)
GUISetState(@SW_SHOW)
$file = FileOpen("words.txt", "r")
$contents = FileRead($file)

$player1 = GUICtrlCreateGroup("Player 1", 290, 5, 110, 50)
    $pScore[1] = GUICtrlCreateLabel("0", 295, 28, 25, 17)
    $pSub1[1] = GUICtrlCreateButton("-1", 320, 25, 20, 20)
    $pAdd1[1] = GUICtrlCreateButton("+1", 345, 25, 20, 20)
    $pAdd2[1] = GUICtrlCreateButton("+2", 370, 25, 20, 20)
    
$player2 = GUICtrlCreateGroup("Player 2", 290, 65, 110, 50)
    $pScore[2] = GUICtrlCreateLabel("0", 295, 88, 25, 17)
    $pSub1[2] = GUICtrlCreateButton("-1", 320, 85, 20, 20)
    $pAdd1[2] = GUICtrlCreateButton("+1", 345, 85, 20, 20)
    $pAdd2[2] = GUICtrlCreateButton("+2", 370, 85, 20, 20)
    
$player3 = GUICtrlCreateGroup("Player 3", 290, 125, 110, 50)
    $pScore[3] = GUICtrlCreateLabel("0", 295, 148, 25, 17)
    $pSub1[3] = GUICtrlCreateButton("-1", 320, 145, 20, 20)
    $pAdd1[3] = GUICtrlCreateButton("+1", 345, 145, 20, 20)
    $pAdd2[3] = GUICtrlCreateButton("+2", 370, 145, 20, 20)
    
$player4 = GUICtrlCreateGroup("Player 4", 290, 185, 110, 50)
    $pScore[4] = GUICtrlCreateLabel("0", 295, 208, 25, 17)
    $pSub1[4] = GUICtrlCreateButton("-1", 320, 205, 20, 20)
    $pAdd1[4] = GUICtrlCreateButton("+1", 345, 205, 20, 20)
    $pAdd2[4] = GUICtrlCreateButton("+2", 370, 205, 20, 20)
    
$player5 = GUICtrlCreateGroup("Player 5", 290, 245, 110, 50)
    $pScore[5] = GUICtrlCreateLabel("0", 295, 268, 25, 17)
    $pSub1[5] = GUICtrlCreateButton("-1", 320, 265, 20, 20)
    $pAdd1[5] = GUICtrlCreateButton("+1", 345, 265, 20, 20)
    $pAdd2[5] = GUICtrlCreateButton("+2", 370, 265, 20, 20)
    
$player6 = GUICtrlCreateGroup("Player 6", 290, 305, 110, 50)
    $pScore[6] = GUICtrlCreateLabel("0", 295, 328, 25, 17)
    $pSub1[6] = GUICtrlCreateButton("-1", 320, 325, 20, 20)
    $pAdd1[6] = GUICtrlCreateButton("+1", 345, 325, 20, 20)
    $pAdd2[6] = GUICtrlCreateButton("+2", 370, 325, 20, 20)
    
$player7 = GUICtrlCreateGroup("Player 7", 290, 365, 110, 50)
    $pScore[7] = GUICtrlCreateLabel("0", 295, 388, 25, 17)
    $pSub1[7] = GUICtrlCreateButton("-1", 320, 385, 20, 20)
    $pAdd1[7] = GUICtrlCreateButton("+1", 345, 385, 20, 20)
    $pAdd2[7] = GUICtrlCreateButton("+2", 370, 385, 20, 20)
    
While 1
    $msg = GUIGetMsg()
    If $msg == $GUI_EVENT_CLOSE Then quit()
    If $msg == $startButton Then getLetters()
    If $msg == $showButton Then getAnswers()
    If $msg == $checkButton Then checkWord()
    If $msg == $doChange1 or $msg == $doChange2 Then getLetters(StringLower(GUICtrlRead($changeLetter1)), StringLower(GUICtrlRead($changeLetter2)))
    For $i = 1 to 7
        If $msg == $pSub1[$i] Then
            updateScore($i, -1)
        EndIf
        If $msg == $pAdd1[$i] Then
            updateScore($i, 1)
        EndIf
        If $msg == $pAdd2[$i] Then
            updateScore($i, 2)
        EndIf
    Next
WEnd
    
Func updateScore($player, $add)
    $score[$player]+=$add
    If $score[$player] < 0 Then
        $score[$player] = 0
    EndIf
    GUICtrlSetData($pScore[$player], $score[$player])
EndFunc
    
Func checkWord()
    $word = GUICtrlRead($checkWord)
    If StringInStr($contents, @CRLF & $word & @CRLF, 2) Then
        GUICtrlSetColor($valid, 0x00FF00)
        GUICtrlSetData($valid, "YES")
    Else
        GUICtrlSetColor($valid, 0xFF0000)
        GUICtrlSetData($valid, "NO")
    EndIf
EndFunc
    
Func getLetters($l1 = -1, $l2 = -1)
    $letters[0][1] = $l1
    $letters[1][1] = $l2
    If $l1 == -1 Then
        $letters[0][1] = $alphabet1[Random(0, 25, 1)]
    EndIf
    If $l2 == -1 Then
        $letters[1][1] = $alphabet2[Random(0, 25, 1)]
    EndIf
    GUICtrlSetData($answersEdit, "")
    GUICtrlSetData($letters[0][0], " " & StringUpper($letters[0][1]))
    GUICtrlSetData($letters[1][0], " " & StringUpper($letters[1][1]))
    GUICtrlSetData($changeLetter1, StringUpper($letters[0][1]))
    GUICtrlSetData($changeLetter2, StringUpper($letters[1][1]))
EndFunc

Func getAnswers()
    $data = ""
    GUICtrlSetData($loading, "Loading...")
    $answers = StringRegExp($contents,"\b(" & $letters[0][1] & "[a-z]*" & $letters[1][1] & ")\b",3)
    If @error Then ; This doesn't work for some reason...
        GUICtrlSetData($loading, "")
        GUICtrlSetData($answersEdit, "None found")
        Return False
    EndIf
    $length = (Ubound($answers) - 1)
    GUICtrlSetData($loading, "")
    For $i = 0 to $length
        If StringLen($answers[$i]) >= 4 Then
            $data&=$answers[$i]
            If $i <> $length Then
                $data&=@CRLF
            EndIf
        EndIf
    Next
    GUICtrlSetData($answersEdit, $data)
EndFunc

Func quit()
    FileClose($file)
    Exit
EndFunc
Edited by magician13134

Share this post


Link to post
Share on other sites

StringRegExp only sets @error when there is a bad pattern or the array is invalid. I'm not sure what the array being invalid means.

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
Sign in to follow this  
Followers 0