bizguy72 Posted October 18, 2009 Share Posted October 18, 2009 (edited) Hi,I am new to AutoIt and was wondering if someone can help me with the following:Let's say that at any one time I might have a variable called $string that at any onepoint in time might contain any of the following combinations:$string = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' ; Three results returned OR$string = 'var lookup_results = "r":["blah"]' ; One result returned OR$string = 'var lookup_results = "r":[ ]' ; No results returned....what I essentially want to do is parse $string and extract allof the values that are between [ and ] (even if there arenone) and then feed that into an array.How would I go about doing that AutoIt? Some type of regex I presume....inPHP I would use the 'preg_match_all' function.Any help would be appreciated.Thanks Edited October 18, 2009 by bizguy72 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 18, 2009 Moderators Share Posted October 18, 2009 (edited) bizguy72, This seems to work for the examples you posted: #include <Array.au3> Global $aPattern[3] = ['["abcd","qwerty 123","67b"]', '["blah"]', '[]'] For $i = 0 To 2 $aResult = StringRegExp($aPattern[$i], "([[:alnum:]| ]+)", 3) If Not IsArray($aResult) Then MsgBox(0, "", "No Result") Else _ArrayDisplay($aResult) EndIf Next But as a relative SRE novice, I have no doubt overlooked something. Never fear, a real expert will come along in a minute or two - putting SRE in a thread title usually works like a magnet! M23 Edit: Welcome to the AutoIt forums, by the way! Edited October 18, 2009 by Melba23 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...
memoryoverflow Posted October 18, 2009 Share Posted October 18, 2009 (edited) Just in case the strings could contain non-alnum characters:#include <array.au3> Local $sString = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' Local $asResult = StringRegExp($sString, '(?:\[|,)(.*?)(?=,|\])', 3) ConsoleWrite($sString & ' contains the substrings: ' & _ArrayToString($asResult) & @CRLF) p.s.: maybe I would have stayed out of it, if I'd seen that beforea real expert will come along in a minute or twoYea, the real ones are still to come (they're here, I know). Edited October 18, 2009 by memoryoverflow (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 18, 2009 Moderators Share Posted October 18, 2009 memoryoverflow, Ok, I will see your pattern and raise with this one: StringRegExp($aPattern[$i], '(?:\["|,")(.*?)(?=",|"\])', 3) This one removes the double quotation marks from the result! 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...
Nutster Posted October 18, 2009 Share Posted October 18, 2009 (edited) Welcome aboard,To grab everything between the quotes and return an array, tryLocal $string = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' ; Three results to return Local $aResult = StringRegExp($string, '\[.*?\]', 3) ; Isolate the part in the brackets Local $I, $sOutput = "" If Not IsArray($aResult) Then MsgBox(0, "Results", "No Output") Else Local $aResult2 = StringRegExp($aResult[0], '"([^"]*)"', 3) For $I = 0 To UBound($aResult2) - 1 $sOutput &= ", " & $aResult2[$I] Next If StringLeft($sOutput, 1) = "," Then $sOutput = StringStripWS(StringMid($sOutput, 2), 3) MsgBox(64, "Results", $sOutput) EndIfIf you want to include the quotes, expand the brackets around [^"]* to include "[^"]*". Don't add new quotes, just move the existing brackets.Edit: Fix coding mistakes (again). Edited October 18, 2009 by Nutster David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd... Link to comment Share on other sites More sharing options...
memoryoverflow Posted October 18, 2009 Share Posted October 18, 2009 If that was the task, fine. Wondering, if an expert comes up with a different structure. (And I think that's the magnet effect for me as non-expert: Learning by contest.) (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 18, 2009 Moderators Share Posted October 18, 2009 (edited) memoryoverflow,I think that's the magnet effect for me as non-expert: Learning by contest.I would prefer to use "friendly banter" rather than "contest", but my sentiments are exactly the same. Anyway, now Nutster has spoken it is probably time for us to retreat back into our shells! M23Edit: Speeling Edited October 18, 2009 by Melba23 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...
bizguy72 Posted October 18, 2009 Author Share Posted October 18, 2009 (edited) Hi everyone, Thanks for the quick responses and thanks for the welcome! Nutster: When I tried your code I get the following error: C:\Documents and Settings\Fred.Smith\Desktop\ab.au3(23,10) : ERROR: syntax error Next $I ~~~~~~~~~^ C:\Documents and Settings\Fred.Smith\Desktop\ab.au3(28,1) : ERROR: missing Next. EndIf ^ C:\Documents and Settings\Fred.Smith\Desktop\ab.au3(21,16) : REF: missing Next. For $I = 0 To ~~~~~~~~~~~~~~~^ C:\Documents and Settings\Fred.Smith\Desktop\ab.au3 - 2 error(s), 0 warning(s) Probably something glaringly obvious but I'm new to this - can you please help? Thanks Welcome aboard, To grab everything between the quotes and return an array, try Local $string = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' ; Three results to return Local $aResult = StringRegExp($string, '[[](?:"(.*?)"(?:,\s*)?)*[]]', 3) Local $I, $sOutput = "" If Not IsArray($aResult) Then MsgBox(0, "Results", "No Output") Else For $I = 0 To UBound($aResult)-1 $sOutput &= ", " & $aResult[$I] Next $I If StringLeft($sOutput, 1) = "," Then $sOutput = StringStripWS(StringMid($sOutput, 2), 3) EndIf MsgBox(64, "Results", $sOutput) EndIf If you want to include the quotes, expand the brackets around .*? to include ".*?". Edited October 18, 2009 by bizguy72 Link to comment Share on other sites More sharing options...
memoryoverflow Posted October 18, 2009 Share Posted October 18, 2009 AutoIt might not like the $I after Next. While it's good to have it there for reading, a ; (comment) before it should help. (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Nutster Posted October 18, 2009 Share Posted October 18, 2009 Oops, that is what I get for coding on the fly and not testing it. I have gone back to my original and fixed it. In AutoIt, Next takes no arguments. David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd... Link to comment Share on other sites More sharing options...
bizguy72 Posted October 18, 2009 Author Share Posted October 18, 2009 Thanks guys - I certainly appreciate your help. There is still a problem though. I had actually looked up the doc for 'For....Next' and worked out that 'Next' doesn't take an argument, but the issue is that only the last value in the string ("67b" in the example) is making it into the array. From Nutster's original code I display the array (last line in code snippet below) and there is only one element "67b" making it into that array: Local $string = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' ; Three results to return Local $aResult = StringRegExp($string, '[[](?:"(.*?)"(?:,\s*)?)*[]]', 3) Local $I, $sOutput = "" _ArrayDisplay($aResult) Thanks Oops, that is what I get for coding on the fly and not testing it. I have gone back to my original and fixed it. In AutoIt, Next takes no arguments. Link to comment Share on other sites More sharing options...
memoryoverflow Posted October 18, 2009 Share Posted October 18, 2009 (edited) ... and there is only one element "67b" making it into that array...Have you tried the other patterns as well? E.g. my and Melba's pattern use (?=... rather than (?:... as last group to catch the last string, too.p.s.: My 4-liner is complete. Just copy it into SciTE and hit F5. Next try: Copy Melba's pattern, paste it over mine and watch the difference. That should give you a good start to see how to use RegExes in AutoIt and refine it to your needs. Edited October 18, 2009 by memoryoverflow (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 18, 2009 Moderators Share Posted October 18, 2009 While it's nice to spend hours coming up with the perfect regex solution, sometimes it's much more reasonable to just go with a simpler approach. Often times ( instead of battling with recursion, negative or positive look aheads), I'll just strip out what I don't need ( my anchor positions ) and work with only the raw data I do. Example:#include <array.au3> Global $s_string = 'var lookup_results = "r":["abcd","qwerty 123","67b"]' Global $a_matches = _GetMyQuotes($s_string) _ArrayDisplay($a_matches) Func _GetMyQuotes($s_string) Local $a_ret[1] ; Remove everythign before and after brackets $s_string = StringRegExpReplace($s_string, "(.*?\[)(.*?)(\].*?(?:\v|\z))", "$2") If @extended = 0 Then Return SetError(1, 0, 0) If $s_string = "" Then Return $a_ret Return StringRegExp($s_string, "\x22(.*?)\x22", 3) EndFunc 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. Link to comment Share on other sites More sharing options...
Nutster Posted October 18, 2009 Share Posted October 18, 2009 (edited) It looks like I screwed up. Sorry about that. I was trying to get several items out, but it seems the pattern kept getting overwritten with the next match. I got too ambitious in my pattern. Sorry about that. I am going back to my original posting and updating the pattern to be closer to Melba23's. I can do this with a two-stage process. The first one isolates the part in the brackets and the next part looks at the part inside. Edited October 18, 2009 by Nutster David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd... Link to comment Share on other sites More sharing options...
bizguy72 Posted October 18, 2009 Author Share Posted October 18, 2009 memoryoverflow and Melba: Yes, I tried your variations and they worked too. Thanks for your assistance. I guess I just like to see the various ways that it can be done and now that I have a few variations I can examine them and go through the doco. SmOke_N: Thank you for your contribution. That works too....goes to show as with most things, that there is more than one way to skin a cat. Nutster: Thanks for your help as well - I appreciate the baseline you provided me with as well. Thanks to all who contributed. You've given me enough info to not only solve my problem but also demonstrated how powerful AutoIt is given that you can take many different approaches to solve a problem. I'm off to digest your examples some more by looking at the docs. 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