Jump to content

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


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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:

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

 

Link to comment
Share on other sites

 

#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
Link to comment
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

 

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

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