Kyan

SRE misbehaving by having a ">" in place of a \w

5 posts in this topic

#1 ·  Posted (edited)

Hi, I don't know why this is happening, but better post the code

#include <Array.au3>
$json = '[{"Id":"2","button":"#random#","status":"ok"},{"Id":"56","button":"#random#","status":"checking"},{"Id":"61","button":"#random#","status":">"},{"Id":"42","button":"#random#",' & _
        '"status":">"},{"Id":"43","button":"#random#","status":"ok"},{"Id":"45","button":"#random#","status":"failed"}]'
$aJSON = StringRegExp($json, '(?i)\{"Id":"(\d+?)","button":".+?","status":"(?:ok|checking)"\}',3)
_ArrayDisplay($aJSON)

output:

2
56
61

expected

2
56
43

I don't know why SRE is capturing 61 :(

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites



Kyan,

Best I can do is to capture the id and the status of each button and then check:

#include <Array.au3>

$json = '[{"Id":"2","button":"#random#","status":"ok"},{"Id":"56","button":"#random#","status":"checking"},{"Id":"61","button":"#random#","status":">"},{"Id":"42","button":"#random#",' & _
        '"status":">"},{"Id":"43","button":"#random#","status":"ok"},{"Id":"45","button":"#random#","status":"failed"}]'

$aJSON = StringRegExp($json, '(?U)"Id":"(\d+)".*"status":"(.*)"', 3) ; :"#random#","status":""

_ArrayDisplay($aJSON, "", Default, 8)

For $i = UBound($aJSON) - 1 To 0 Step -2
    $sStatus = $aJSON[$i]
    _ArrayDelete($aJSON, $i)
    Switch $sStatus

        Case "ok", "checking"
            ; Do nothing
        Case Else
            _ArrayDelete($aJSON, $i - 1)
    EndSwitch

Next

_ArrayDisplay($aJSON, "", Default, 8)

No doubt an SRE guru will be along shortly with something really clever.

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

#3 ·  Posted (edited)

 

#include <Array.au3>

$json = '[{"Id":"2","button":"#random#","status":"ok"},{"Id":"56","button":"#random#","status":"checking"},{"Id":"61","button":"#random#","status":">"},{"Id":"42","button":"#random#",' & _
        '"status":">"},{"Id":"43","button":"#random#","status":"ok"},{"Id":"45","button":"#random#","status":"failed"}]'

$aJSON = StringRegExp($json, '(?i)"Id":"(\d+)[^}]+(?:ok|checking)', 3)

_ArrayDisplay($aJSON)

Edit : forgot the comment  :)
The negated character class [^}]+  will make the search restart at the current position as soon as a  }  is found

 

Edited by mikell

Share this post


Link to post
Share on other sites

Kyan

Hopefully, the block comment in this script explains the problem you are having with ".+?" in your RE pattern.

#include <Array.au3>
$json = '[{"Id":"2","button":"#random#","status":"ok"},' & _
        '{"Id":"56","button":"#random#","status":"checking"},' & _
        '{"Id":"61","button":"#random#","status":">"},' & _
        '{"Id":"42","button":"#random#","status":">"},' & _
        '{"Id":"43","button":"#random#","status":"ok"},' & _
        '{"Id":"45","button":"#random#","status":"failed"}]'

$aJSON = StringRegExp($json, '(?i)\{"Id":"(\d+?)","button":"[^:]+:"(?:ok|checking)"\}', 3)

#cs
    $aJSON = StringRegExp($json, '(?i)\{"Id":"(\d+?)","button":".+?","status":"(?:ok|checking)',3)

    \{"Id":"(\d+?)","button":"      matches {"Id":"61","button":"   with 61 captured;

    .+?","status":"(?:ok|checking)  means match all characters until the first occurrence of ","status":"(?:ok|checking) is matched.
    So .+? matches:-
                          #random#","status":">"},' & _
    '{"Id":"42","button":"#random#","status":">"},' & _
    '{"Id":"43","button":"#random#","status":"ok"}

#ce

_ArrayDisplay($aJSON)
#cs ;Returns:-
    2
    56
    43
#ce

 

1 person likes this

Share this post


Link to post
Share on other sites

Malkey, so SRE is not delimited by the text you enter? (thought it would be constrained inside the curve brackets "\{\}")

So, if it behaves like a inline script is more complex to imagine how it is gonna to behave :|, can this be done with lookahead?

 


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

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