Jump to content

StringRegExp


Recommended Posts

Can some expert with regular expression help me to obtain expected data from a string like that:

"scans": {

"CLEAN MX": {"detected": false, "result": "clean site"},

"TrendMicro": {"detected": false, "result": "clean site"},

"Yandex Safebrowsing": {"detected": false, "result": "clean site"},

"Phishtank": {"detected": false, "result": "clean site"},

"MalwareDomainList": {"detected": false, "result": "clean site"},

"VX Vault": {"detected": false, "result": "clean site"},

"SCUMWARE.org": {"detected": false, "result": "clean site"},

"Google Safebrowsing": {"detected": false, "result": "clean site"},

"Malc0de Database": {"detected": false, "result": "clean site"},

"SpyEyeTracker": {"detected": false, "result": "clean site"},

"BitDefender": {"detected": false, "result": "clean site"},

"Opera": {"detected": false, "result": "clean site"},

"ParetoLogic": {"detected": false, "result": "clean site"},

"Avira": {"detected": false, "result": "clean site"},

"URLQuery": {"detected": false, "result": "unrated site"},

"G-Data": {"detected": false, "result": "clean site"},

"Wepawet": {"detected": false, "result": "unrated site"},

"ZeusTracker": {"detected": false, "result": "clean site"}

}

What I try to get is the name, detected & result. Something like:

CLEAN MX false clean site

Any idea?

When the words fail... music speaks.

Link to comment
Share on other sites

I'm not a pro, but here how i would have made it:

#Include <Array.au3>
#Include <String.au3>
$str = '"scans": {' & _
'"CLEAN MX": {"detected": false, "result": "clean site"},' & _
'"TrendMicro": {"detected": false, "result": "clean site"},' & _
'"Yandex Safebrowsing": {"detected": false, "result": "clean site"},' & _
'"Phishtank": {"detected": false, "result": "clean site"},' & _
'"MalwareDomainList": {"detected": false, "result": "clean site"},' & _
'"VX Vault": {"detected": false, "result": "clean site"},' & _
'"SCUMWARE.org": {"detected": false, "result": "clean site"},' & _
'"Google Safebrowsing": {"detected": false, "result": "clean site"},' & _
'"Malc0de Database": {"detected": false, "result": "clean site"},' & _
'"SpyEyeTracker": {"detected": false, "result": "clean site"},' & _
'"BitDefender": {"detected": false, "result": "clean site"},' & _
'"Opera": {"detected": false, "result": "clean site"},' & _
'"ParetoLogic": {"detected": false, "result": "clean site"},' & _
'"Avira": {"detected": false, "result": "clean site"},' & _
'"URLQuery": {"detected": false, "result": "unrated site"},' & _
'"G-Data": {"detected": false, "result": "clean site"},' & _
'"Wepawet": {"detected": false, "result": "unrated site"},' & _
'"ZeusTracker": {"detected": false, "result": "clean site"}' & _
'}'

$str = StringRegExp($str, '"scans": {(.*)}', 3)
$str = $str[0]
$reg = StringRegExp($str, '"(.*?)": {".*?": (.*?), ".*?": "(.*?)"}', 3)
$ub = UBound($reg)
ConsoleWrite($ub & @CRLF)
Global $result[1][3] = [[0, "", ""]]
For $i = 0 To $ub - 1 Step +3
ReDim $result[$result[0][0] + 2][3]
$result[0][0] += 1
$result[$result[0][0]][0] = $reg[$i]
$result[$result[0][0]][1] = $reg[$i + 1]
$result[$result[0][0]][2] = $reg[$i + 2]
Next
_ArrayDisplay($result)

Me too, i'm waiting for pros to show us some better ways!

Link to comment
Share on other sites

Instead of using

.*?

you can use

[^"]*

which is easier on the regex engine because it doesn't need to backtrack.

EDIT: except for the boolean that isn't in quotes. In that case, just use [a-z]*

EDIT 2: Now that I look at it, there's no reason to use regex for the "detected," and "result" text.

$reg = StringRegExp($str, '"([^"]*)": {"detected": ([a-z]*), "result": "([^"]*)"}', 3)
Edited by GPinzone
Gerard J. Pinzonegpinzone AT yahoo.com
Link to comment
Share on other sites

Here's my take on it:

#include <Array.au3>
$sString = '"scans": {' & @CRLF & _
'"CLEAN MX": {"detected": false, "result": "clean site"},' & @CRLF & _
'"TrendMicro": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Yandex Safebrowsing": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Phishtank": {"detected": false, "result": "clean site"},' & @CRLF & _
'"MalwareDomainList": {"detected": false, "result": "clean site"},' & @CRLF & _
'"VX Vault": {"detected": false, "result": "clean site"},' & @CRLF & _
'"SCUMWARE.org": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Google Safebrowsing": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Malc0de Database": {"detected": false, "result": "clean site"},' & @CRLF & _
'"SpyEyeTracker": {"detected": false, "result": "clean site"},' & @CRLF & _
'"BitDefender": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Opera": {"detected": false, "result": "clean site"},' & @CRLF & _
'"ParetoLogic": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Avira": {"detected": false, "result": "clean site"},' & @CRLF & _
'"URLQuery": {"detected": false, "result": "unrated site"},' & @CRLF & _
'"G-Data": {"detected": false, "result": "clean site"},' & @CRLF & _
'"Wepawet": {"detected": false, "result": "unrated site"},' & @CRLF & _
'"ZeusTracker": {"detected": false, "result": "clean site"}' & @CRLF & _
'}'
Local $iOffset = 1
Local $asArray[1][3], $asTemp
$iCount = 1
While 1
    $asTemp = StringRegExp($sString, '"([^"]*)": {"detected": ([a-z]*), "result": "([^"]*)"}', 1, $iOffset)
    If @error = 0 Then
        $iOffset = @extended
    Else
        ExitLoop
    EndIf
    ReDim $asArray[$iCount][3]
    For $i = 0 To UBound($asTemp) - 1
        $asArray[$iCount - 1][$i] = $asTemp[$i]
    Next
    $iCount += 1
WEnd
_ArrayDisplay($asArray)

Edited by GMK
Link to comment
Share on other sites

Instead of using

.*?

you can use

[^"]*

which is easier on the regex engine because it doesn't need to backtrack.

Hang on, doesnt regex use FSA (Finite State Automata) algorithm to build the search tree and process the input? why does it need to backtrack?

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

Regexp engines are either DFA (deterministic finete-state automaton) or NFA (non-DFA). PCRE includes both engines internally but only the NFA is accessible by means of StringRegExp[Replace].

A DFA never backtracks, contrary to the (our) NFA.

Backtracking is most important when quantifiers appear (+, *, {n, p}) with or without greediness, where almost infinite loops can be triggered.

Hopefully, PCRE is very advanced and embarks a large number of optimizations to detect and avoid uselessly long backtracking.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

BTW using JSON UDF for such simple task would be overkill IMHO.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...