Jump to content
Sign in to follow this  
Andreik

StringRegExp

Recommended Posts

Andreik

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

Share this post


Link to post
Share on other sites
matwachich

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!

Share this post


Link to post
Share on other sites
Andreik

Thanks a lot, it works nice. I never knew very well how to use this regular expressions.


When the words fail... music speaks

Share this post


Link to post
Share on other sites
GPinzone

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

Share this post


Link to post
Share on other sites
GMK

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

Share this post


Link to post
Share on other sites
JohnQSmith

That looks like JSON. You might take a look at the JSON UDF Library.


Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Share this post


Link to post
Share on other sites
twitchyliquid64

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

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Andreik

Thanks GPinzone & GMK for your help.

@JohnQSmith yes, it's JSON but I don't know how to use the UDF


When the words fail... music speaks

Share this post


Link to post
Share on other sites
jchd

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)

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  

  • Similar Content

    • Rocker
      By Rocker
      I apologize for my poor coding skills, but only on occasion do I have a need to automate a repetitious task to save time.  In a nutshell the following code is supposed to go to a website, save the site using Scrapbook (a plugin used by an older version of Firefox), rename the folder in Windows Explorer that Scrapbook assigns a random name for and then moves on to the next website, of which there are hundreds.  Everything works, except after the address bar gets populated with the URL, it's like the ENTER key never gets pushed.  The weird thing is, this worked yesterday before I left to go home and when I came in this morning it didn't work.  The next thing I'll be trying to learn is to loop this with a the list of the websites.  Thank you for your help!
      WinActivate( "[CLASS:MozillaWindowClass]" ) ;Apply focus to the open Mozilla firefox window Sleep(500) Send("^k") ;Apply focus to the address bar Sleep(500) Send("www.WEBSITE.com {ENTER}") ;Send the website to the address bar Sleep(10000) ;Wait for the website to fully open Send("!c") ;Opens the drop down menu for Scrapbook Sleep(2000) Send("{ENTER}") ;Selects the default drop down selection of save Sleep(20000) WinActivate( "[CLASS:CabinetWClass]" ) ;Applies focus to the open Windows Explorer window where the websites are saved Sleep(1000) Send("{SPACE}") ; Selects a folder in the folders list Sleep(200) Send("{HOME}") ;Moves to the top folder Sleep(200) Send("{F2}") ;Keyboard shortcut to rename the folder Sleep(200) Send("www.WEBSITE.com {ENTER}") ;Applies the new name to the folder  
    • Parsix
      By Parsix
      Hi,
      how to use twain scanning in my app with custom settings
      1. output format : jpg
      2. compressing jpg
      3. mx res : 100
      4.custom filename path (examle: D:\123.jpg)
      5. get scanner list and scan with selected scanner (get scanner list in my combo or any ctrl)
      6. free version
       
       
      eztwain free tested
    • nacerbaaziz
      By nacerbaaziz
      Hello all
      I have a question please
      I have a link to an .exe file
      for example this link
      https://www.nvaccess.org/files/nvda/releases/2017.1/NVDA_2017.1.exe
      I want to check if the link is exists orno
      surely  Without downloading the file
      is there any  function or a way to  do this?
      Thanks in advance
    • TheSaint
      By TheSaint
      I've finally come up with a simple solution for dragging & dropping a URL.
      It's not perfect, but it does the job. Mostly just a tweak of some Help file examples.
      Thanks also to AutoBert, for his StringRegExp and example that set me on the right path.
      I have seen this requested for many times, so enjoy!
      It goes without saying, that any improvements are welcome.
      Dropbox For URLs.au3
      #include <GUIConstantsEx.au3> #include <GuiRichEdit.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Global $hGui, $g_hRichEdit Example() Func Example() Local $cnt, $URL, $val, $text Local $iMsg, $state = $WS_CAPTION + $WS_POPUP + $WS_CLIPSIBLINGS + $WS_SYSMENU $hGui = GUICreate(StringTrimRight(@ScriptName, 4), 160, 100, -1, -1, $state, $WS_EX_TOPMOST) $g_hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 140, 80, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) GUISetState(@SW_SHOW) _GUICtrlRichEdit_SetEventMask($g_hRichEdit, $ENM_LINK) _GUICtrlRichEdit_AutoDetectURL($g_hRichEdit, True) $cnt = 0 While True $iMsg = GUIGetMsg() Select Case $iMsg = $GUI_EVENT_CLOSE _GUICtrlRichEdit_Destroy($g_hRichEdit) ExitLoop Case Else If $cnt = 20 Then $text = _GUICtrlRichEdit_GetText($g_hRichEdit) If $text <> "" Then $val = StringRegExp($text, "(?s)((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)", 2) If UBound($val) > 0 Then $URL = $text ClipPut($URL) ;MsgBox(0, "URL", $URL, 0, $hGui) Else _GUICtrlRichEdit_SetText($g_hRichEdit, "Only URL's are supported!") ;MsgBox(0, "Not URL", "Only URL's are supported!", 0, $hGui) EndIf Sleep(600) _GUICtrlRichEdit_SetText($g_hRichEdit, "") EndIf $cnt = 0 Else $cnt = $cnt + 1 EndIf EndSelect WEnd EndFunc ;==>Example GUIDelete($hGui) Exit I also wanted a version that would work with an earlier version of AutoIt (v3.3.0.0), to use with updates for some older scripts (programs).
      The following works fine on my Win XP laptop, but fails (also when compiled) on my Win 7 Netbook.
      I found a RichEdit example in the GUICtrlCreateEdit function (Example 2) in the AutoIt v3.3.0.0 Help file, and modified that to suit my script.
      Failure is no doubt due to 'MSCOMCT2.OCX' missing from the System32 folder, at the very least.
      Any solution to getting the following to work on Win 7 would be greatly appreciated. Perhaps a different '.OCX' file etc.
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> Global $oMyError RichEditExample() Exit ; Rich edit control EXAMPLE using GUICtrlCreateObj ; Author: Kåre Johansson ; AutoIt Version: 3.1.1.55 ; Description: Very Simple example: Embedding RICHTEXT object ; Needs: MSCOMCT2.OCX in system32 but it's probably already there ; Date: 3 jul 2005 Func RichEditExample() Local $cnt, $DropBoxGUI, $GUIActiveX, $msg, $oRP, $state, $text, $URL, $val $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") $oRP = ObjCreate("RICHTEXT.RichtextCtrl.1") If @error Then Return $state = $WS_CAPTION + $WS_POPUP + $WS_CLIPSIBLINGS + $WS_SYSMENU $DropBoxGUI = GUICreate(StringTrimRight(@ScriptName, 4), 160, 100, -1, -1, $state, $WS_EX_TOPMOST) $GUIActiveX = GUICtrlCreateObj($oRP, 0, 0, 140, 80) GUICtrlSetPos($GUIActiveX, 5, 5, 150, 90) $cnt = 0 With $oRP; Object tag pool .OLEDrag() .Font = 'Arial' .Text = @CRLF & @CRLF & " Drag & Drop a URL here." .SelBold = True .BackColor = 0xff00 ;.FileName = @ScriptDir & '\RichText.rtf' ;$oRP.AboutBox() ;$oRP.SelBold = False ;$oRP.SelItalic = False ;$oRP.SelUnderline = False ;$oRP.SelFontSize = 8 ;$oRP.SaveFile(@ScriptDir & "\RichText.rtf", 0) EndWith GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE GUIDelete($DropBoxGUI) ExitLoop Case Else If $cnt = 20 Then $text = $oRP.Text If $text <> "" Then If $text = @CRLF & @CRLF & " Drag & Drop a URL here." Then Sleep(2000) Else $val = StringRegExp($text, "(?s)((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)", 2) If UBound($val) > 0 Then $URL = $text ClipPut($URL) ;MsgBox(0, "URL", $URL, 0, $hGui) Sleep(600) Else With $oRP .Text = "Only URL's are supported!" EndWith ;MsgBox(0, "Not URL", "Only URL's are supported!", 0, $hGui) Sleep(1500) EndIf EndIf With $oRP .Text = "" .SelBold = False EndWith EndIf $cnt = 0 Else $cnt = $cnt + 1 EndIf EndSelect WEnd EndFunc ;==>RichEditExample Func MyErrFunc() MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _ "err.description is: " & @TAB & $oMyError.description & @CRLF & _ "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _ "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _ "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _ "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _ "err.source is: " & @TAB & $oMyError.source & @CRLF & _ "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _ "err.helpcontext is: " & @TAB & $oMyError.helpcontext , 5) ; Will automatically continue after 5 seconds Local $err = $oMyError.number If $err = 0 Then $err = -1 SetError($err) ; to check for after this function returns EndFunc ;==>MyErrFunc And a slower embedded IE based version. This has an error in latest AutoIt, but works fine in v3.3.0.0.
      #include <Constants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <IE.au3> Local $DropboxGUI, $GUIActiveX, $loaded, $loops, $oBody, $oIE, $sHTML, $timeout, $urlpage _IEErrorHandlerRegister() $oIE = _IECreateEmbedded() $DropboxGUI = GUICreate("URL Dropbox", 140, 140, -1, -1, _ $WS_OVERLAPPED + $WS_VISIBLE + $WS_CLIPSIBLINGS + $WS_SYSMENU, $WS_EX_TOPMOST) $GUIActiveX = GUICtrlCreateObj($oIE, 5, 5, 125, 100) $oBody = _IETagNameGetCollection($oIE, "body", 0) _IEDocInsertHTML($oBody, "", "afterbegin") $loaded = _IEPropertyGet($oIE, "locationurl") $urlpage = $loaded $loops = 0 $sHTML = "" $sHTML &= "<HTML>" & @CR $sHTML &= "<HEAD>" & @CR $sHTML &= "<TITLE>Drag And Drop Example</TITLE>" & @CR $sHTML &= "</HEAD>" & @CR $sHTML &= "<BODY>" & @CR $sHTML &= "</BODY>" & @CR $sHTML &= "</HTML>" _IELoadWaitTimeout(1) While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ; Quit, Close or Exit window GUIDelete($DropboxGUI) ExitLoop Case Else ;;; If $loops = 10 Then $loops = 0 $loaded = _IEPropertyGet($oIE, "locationurl") If $loaded <> $urlpage Then _IEAction($oIE, "stop") _IEDocWriteHTML($oIE, $sHTML) If $loaded = "res://ieframe.dll/navcancl.htm" Then $loaded = "" $urlpage = $loaded _IEBodyWriteHTML($oIE, '<p style="WORD-WRAP: normal; WORD-BREAK: break-all; FONT-SIZE: 8px">' & $loaded & '</p>') EndIf Else $loops = $loops + 1 EndIf EndSelect WEnd Exit  
    • kanishk619
      By kanishk619
      The following code contains 2 functions to achieve the same results, although function2 works fine whereas function1 returns different results
      ;#include <Array.au3> #include <security.au3> #include <WinAPI.au3> #include <ProcessConstants.au3> Global Const $TOKEN_MAXIMUM_ALLOWED = 0x02000000 Func _GetTokenPSid($hToken) Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1) $tempPtr = DllStructCreate("PTR") $ptrSize = DllStructGetSize($tempPtr) $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall)) $mem = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]") DllStructSetData($mem, "Attributes", $rawSid) $pSid = DllStructGetPtr($mem) Return $pSid EndFunc ;==>_GetTokenPSid Func _GetTokenUser1($hToken) $pSid = _GetTokenPSid($hToken) Local $aCall = DllCall("advapi32.dll", "bool", "LookupAccountSidW", "ptr", "", "ptr", $pSid, "wstr", "", "dword*", 65536, "wstr", "", "dword*", 65536, "int*", 0) If IsArray($aCall) Then Return $aCall[5] & "\" & $aCall[3] Else Return "" EndIf EndFunc ;==>_GetTokenUser Func _GetTokenUser2($hToken) Local $aCall = DllStructGetData(_Security__GetTokenInformation($hToken, $TOKENUSER), 1) $tempPtr = DllStructCreate("PTR") $ptrSize = DllStructGetSize($tempPtr) $rawSid = BinaryMid($aCall, $ptrSize * 2 + 1, BinaryLen($aCall)) $mem = DllStructCreate("byte Attributes[" & BinaryLen($rawSid) & "]") DllStructSetData($mem, "Attributes", $rawSid) $pSid = DllStructGetPtr($mem) Local $bCall = DllCall("advapi32.dll", "bool", "LookupAccountSidW", "ptr", "", "ptr", $pSID, "wstr", "", "dword*", 65536, "wstr", "", "dword*", 65536, "int*", 0) If IsArray($bCall) Then Return $bCall[5] & "\" & $bCall[3] Else Return "" EndIf EndFunc Func _ProcessTokenInfo($pid) $hToken = _Security__OpenProcessToken(_WinAPI_OpenProcess($TOKEN_MAXIMUM_ALLOWED, 0, $pid), $TOKEN_QUERY) If Not $hToken Then $hToken = _Security__OpenProcessToken(_WinAPI_OpenProcess($PROCESS_QUERY_LIMITED_INFORMATION, 0, $pid), $TOKEN_QUERY) EndIf ConsoleWrite("GetTokenUser1 : " & _GetTokenUser1($hToken) & @CRLF) ConsoleWrite("GetTokenUser2 : " & _GetTokenUser2($hToken) & @CRLF) ConsoleWrite(@CRLF) _WinAPI_CloseHandle($hToken) Return EndFunc ;==>_ProcessTokenInfo For $i = 1 To 10 _ProcessTokenInfo(856) Next Below are the results
      Am I doing something wrong here ?
×