Sign in to follow this  
Followers 0
bizguy72

Regular expression help

15 posts in this topic

#1 ·  Posted (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 one

point 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 all

of the values that are between [ and ] (even if there are

none) and then feed that into an array.

How would I go about doing that AutoIt? Some type of regex I presume....in

PHP I would use the 'preg_match_all' function.

Any help would be appreciated.

Thanks

Edited by bizguy72

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

#3 ·  Posted (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 before

a real expert will come along in a minute or two

Yea, the real ones are still to come (they're here, I know). :) Edited by memoryoverflow

(The signature is placed on the back of this page to not disturb the flow of the thread.)

Share this post


Link to post
Share on other sites

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._______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

#5 ·  Posted (edited)

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, '\[.*?\]', 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)
EndIf

If 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 by Nutster

David Nuttall
Nuttall 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...

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites

#7 ·  Posted (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! :)

M23

Edit: Speeling

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

#8 ·  Posted (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 by bizguy72

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites

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 Nuttall
Nuttall 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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by memoryoverflow

(The signature is placed on the back of this page to not disturb the flow of the thread.)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#14 ·  Posted (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 by Nutster

David Nuttall
Nuttall 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...

Share this post


Link to post
Share on other sites

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. :)

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