Sign in to follow this  
Followers 0
schnibble

StringRegExp search any case of letter combination in a txt file

10 posts in this topic

i try to get e StringRegExp which search all combinations of words in a list with some letters.

test

text

guest

.....

and search with this letters:

gtestx

as result i should get

test

text

and search with this letters:

gtesux

as result i should get

guest

(not text and not test! here are two t's needed)

is this possible?

Share this post


Link to post
Share on other sites



i try to get e StringRegExp which search all combinations of words in a list with some letters.

test

text

guest

.....

and search with this letters:

gtestx

as result i should get

test

text

and search with this letters:

gtesux

as result i should get

guest

(not text and not test! here are two t's needed)

is this possible?

Not without writing the proper algorithms yourself. There's no option I'm aware of like this with RegExp, and would make no sense for there to be.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

The regular expression pattern has to be specifically taylored for what it is searching for. More structured than you described in your first post.

This regular expression pattern appears to return all occurrences of "text, test, and guest" from the sample target string.

With this pattern as a basis, you should be able to change this pattern to search for the others words mentioned in your first post.

The AutoIt help file should tell you what the characters is the pattern are doing. And with experimenting you should be able to work out what the help file is on about.

I am no regular expression expert. I was surprised then this pattern worked. I am still reading and experimenting with regular expression.

Local $sStr = "C:\Documents quest and text guest settings test " & _
        "local text settings " & @CRLF & "temt quest Text Guest"
        
; The two unchanging letters are "e" and the last "t" in guest, text, test  
; The second last letter can be either "x" or "s".
; And the search words start with either "gu" or(|) "t"
$aResult = StringRegExp($sStr, "((?:[gu]{2}|[t])e[xs]t)", 3)        
        
; Allows for the search words starting with upper case
;$aResult = StringRegExp($sStr, "((?:[Ggu]{2}|[tT])e[xs]t)", 3)

Local $res
If IsArray($aResult) Then
    For $x = 0 To UBound($aResult) - 1
        $res &= $x & " = " & $aResult[$x] & @CRLF
    Next
    MsgBox(0, "Is Array", $res)
Else
    MsgBox(0, "Not Array", $aResult)
EndIf

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The regular expression pattern has to be specifically taylored for what it is searching for. More structured than you described in your first post.

This regular expression pattern appears to return all occurrences of "text, test, and guest" from the sample target string.

With this pattern as a basis, you should be able to change this pattern to search for the others words mentioned in your first post.

The AutoIt help file should tell you what the characters is the pattern are doing. And with experimenting you should be able to work out what the help file is on about.

I am no regular expression expert. I was surprised then this pattern worked. I am still reading and experimenting with regular expression.

Local $sStr = "C:\Documents quest and text guest settings test " & _
        "local text settings " & @CRLF & "temt quest Text Guest"
        
; The two unchanging letters are "e" and the last "t" in guest, text, test  
; The second last letter can be either "x" or "s".
; And the search words start with either "gu" or(|) "t"
$aResult = StringRegExp($sStr, "((?:[gu]{2}|[t])e[xs]t)", 3)        
        
; Allows for the search words starting with upper case
;$aResult = StringRegExp($sStr, "((?:[Ggu]{2}|[tT])e[xs]t)", 3)

Local $res
If IsArray($aResult) Then
    For $x = 0 To UBound($aResult) - 1
        $res &= $x & " = " & $aResult[$x] & @CRLF
    Next
    MsgBox(0, "Is Array", $res)
Else
    MsgBox(0, "Not Array", $aResult)
EndIf
Not quite sure how you can follow that logic with his last requested type of distinction:
Local $sStr = "txeusg"
$aResult = StringRegExp($sStr, "((?:[gu]{2}|[t])e[xs]t)", 3)    
Local $res
If IsArray($aResult) Then
    For $x = 0 To UBound($aResult) - 1
        $res &= $x & " = " & $aResult[$x] & @CRLF
    Next
    MsgBox(0, "Is Array", $res)
Else
    MsgBox(0, "Not Array", $aResult)
EndIf

I don't see anyway to do it without looping through it, then (IMO) you're just using the wrong tool when you could do something so simple as:

Local $s_str = "txeusg"

If _WordMatch($s_str, "Text") Then
    MsgBox(64, "Info", "Text was found")
ElseIf _WordMatch($s_str, "guest") Then
    MsgBox(64, "Info", "guest was found")
Else
    MsgBox(16, "Error", "No words matched")
EndIf

Func _WordMatch($s_str_in, $s_match_find, $i_case = 0)
    Local $a_split = StringSplit($s_match_find, "")
    Local $s_hold, $i_extended
    
    For $i = 1 To $a_split[0]
        If StringInStr("," & $s_hold, "," & $a_split[$i] & ",", $i_case) = 0 Then
    ; How many times was that char found in the string we want to match
            StringReplace($s_match_find, $a_split[$i], "", Default, $i_case)
            $i_extended = @extended
    ; If there are not enough chars, or the char doesn't exist, then no match return error
            StringReplace($s_str_in, $a_split[$i], "", Default, $i_case)
            If @extended < $i_extended Then Return SetError(1, 0, 0)
            $s_hold &= $a_split[$i] & ","
        EndIf
    Next
    
    Return 1
EndFunc
I think that would be much more proficient and less error prone. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

the idea behind my question is, i try to make a Word Challange (http://www.playfish.com/?page=game_wordchallenge) script who solve the word word search ;-)

i get 6 letters and search all possible words with this letters (which change in every round)

thanks @ SmOke_N i think that will help me

Edited by schnibble

Share this post


Link to post
Share on other sites

i saw the script is too slow for solve it fast enough..

Share this post


Link to post
Share on other sites

i saw the script is too slow for solve it fast enough..

We offered a solution based off of what you originally asked. Not some speed demon to solve 1000's of words dictionary for some word puzzle in record time... Sorry, I don't see RegEx being any faster due to it having to be in a loop as well.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Hi,

how many words do you have to match against the 6 letters? Maybe there is a faster solution possible.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This way is 30% faster:

;----- small dictionary to test -----
Global $asDictionary[573] = ["","sap","sat","sad","rat","rap","ram","rag", _
"nap","Nat","mat","map","mad","lap","lag","lad","fat","fan","fad","fin","fit","lid", _
"lip","lit","mid","mitt","nit","nip","rid","rig","rim","rip","Sid","sin","sip","log", _
"mom","mop","nod","rod","Ron","rot","sod","fun","mud","mum","nut","rug","rut","sum", _
"sun","fed","led","leg","met","Ned","net","bag","bad","bam","bat","cap","cab","dad", _
"Dan","gas","gag","ham","hat","jab","jam","pan","pat","tab","tag","tan","tap","bid", _
"dig","dip","hid","hit","hip","Jim","jig","kin","kid","pin","pit","pig","tin","tip", _
"Tim","cop","con","Don","dog","hop","hog","job","jog","pot","pop","top","Tom","bug", _
"bud","bum","cup","cub","dud","dug","Gus","gun","hum","jug","pup","tub","tug","beg", _
"bed","bet","hen","jet","Ken","pen","pet","peg","pep","the","of","and","a","to", _
"in","is","you","that","it","he","was","for","on","are","as","with","his","they", _
"I","at","be","this","have","from","or","one","had","by","word","but","not","what", _
"all","were","we","when","your","can","said","there","use","an","each","which","she","do", _
"how","their","if","will","up","other","about","out","many","then","them","these","so","some", _
"her","would","make","like","him","into","time","has","look","two","more","write","go","see", _
"number","no","way","could","people","my","than","first","water","been","call","who","oil","its", _
"now","find","long","down","day","did","get","come","made","may","part","over","new","sound", _
"take","only","little","work","know","place","year","live","me","back","give","most","very","after", _
"thing","our","just","name","good","sentence","man","think","say","great","where","help","through","much", _
"before","line","right","too","mean","old","any","same","tell","boy","follow","came","want","show", _
"also","around","farm","three","small","set","put","end","does","another","well","large","must","big", _
"even","such","because","turn","here","why","ask","went","men","read","need","land","different","home", _
"us","move","try","kind","hand","picture","again","change","off","play","spell","air","away","animal", _
"house","point","page","letter","mother","answer","found","study","still","learn","should","America","world", _
"every","near","add","food","between","own","below","country","plant","last","school","father","keep","tree", _
"never","start","city","earth","eye","light","thought","head","under","story","saw","left","don't","few", _
"while","along","might","chose","something","seem","next","hard","open","example","begin","life","always", _
"both","paper","together","got","group","often","run","important","until","children","side","feet","car", _
"night","walk","white","sea","began","grow","took","river","four","carry","state","once","book","hear", _
"stop","without","second","late","miss","idea","enough","eat","face","watch","far","Indian","really","almost", _
"let","above","girl","sometimes","mountain","cut","young","talk","soon","list","song","being","leave", _
"it's","am","ate","best","better","black","blue","bring","brown","buy","clean","cold","done","draw", _
"drink","eight","fall","fast","five","fly","full","funny","gave","giving","goes","green","hold","hot", _
"hurt","jump","laugh","myself","pick","please","pretty","pull","ran","red","ride","round","seven","shall", _
"sing","sit","six","sleep","ten","thank","today","upon","warm","wash","wish","yellow","yes","act", _
"ant","bake","band","bank","bell","belt","Ben","bend","bent","Bess","bike","bit","bite","blast", _
"bled","blend","blimp","blink","bliss","block","blond","blot","bluff","blunt","bone","brag","brand","brass", _
"brat","bred","bride","brig","brim","broke","brunt","brute","bump","bun","bunt","bust","camp","cane", _
"can't","cape","cast","cat","clad","clam","clamp","clan","clap","clasp","class","cliff","cling","clink", _
"clip","close","clot","club","clump","clung","cone","crab","craft","cram","cramp","crib","crime","crisp", _
"crop","crust","cure","cute","dam","damp","den","dent","dim","dime","dine","dire","dive","dope", _
"draft","drag","drank","dress","drift","high","those","mile","family"]


;----- Test string -----
$sTest = "gtesux"

;----- prepare test string as array -----
$asTestArray = StringSplit($sTest,"")

;----- loop through dictionary -----
For $i = 1 to (UBound($asDictionary) - 1)
    
    $sHold = $asDictionary[$i]
    
    For $j = 1 to (UBound($asTestArray) - 1)
        $sHold = StringReplace($sHold,$asTestArray[$j],"",1)
    Next
    
    If StringLen($sHold) > 0 Then ContinueLoop
    
    ConsoleWrite($asDictionary[$i] & @LF)
    
Next

... it loops through the test string rather than each individual word.

[EDIT] bloody line-wrapping :)

Edited by andybiochem

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

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