Jump to content

Recommended Posts

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

  Reveal hidden contents

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

  • Moderators
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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:

  Reveal hidden contents

 

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
Posted

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

 

Posted

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

  Reveal hidden contents

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...