Jump to content

Stringregexp?


Recommended Posts

Ive got this function Im using to parse some text

Func _Get_List($aParams)
    $sList_URL = $aParams[0] & "?list=1" & "&token=" & $aParams[1]
    $temp = _StringBetween(StringRegExpReplace(BinaryToString(InetRead($sList_URL, 1)), "(\s)", ""), "[[", "]]")
    $aList = StringSplit($temp[0], "],[", 3);<<<<<<< This line
    Local $aFinalList[19][UBound($aList)]
    For $i = 0 To UBound($aFinalList, 2) - 1
        $atemp = StringSplit($aList[$i], ",", 2)
        For $j = 0 To 18
            $aFinalList[$j][$i] = $atemp[$j]
        Next
    Next
    Return $aFinalList
EndFunc

The problem is with an array returned from _StringBetween(), (commented in code above) where It only works for me if I use $temp[0], but will not work for someone else unless they use $temp[1], which gives me an array out of bounds error.

The array is created with this line

$temp = _StringBetween(StringRegExpReplace(BinaryToString(InetRead($sList_URL, 1)), "(\s)", ""), "[[", "]]")

and the BinaryToString(InetRead($sList_URL, 1)) returns some text, here is an example of the string it might return JSON format I think

{"build":19648,"label": [
]
,"torrents": [

["597E3137030DF57F5099C834F00E360A3BD593E0",152,"Breaking.Bad.S03E11.Abiquiu.HDTV.XviD-FQM",373215148,1000,373215148,0,0,0,0,0,"",0,0,0,0,65536,-1,0],
["ACEE50718653578A8000D9FF864F448CDFB968F2",152,"Breaking.Bad.S03E12.Half.Measures.HDTV.XviD-FQM",378552942,1000,378552942,360448,0,0,0,0,"",0,0,0,0,65536,-1,0],
["1F4270D1A24DABD05B4E103AD11FA55B4BA71706",201,"Breaking.Bad.S03E13.Full.Measure.HDTV.XviD-FQM",377013305,1000,367576121,71450624,194,0,0,-1,"",0,40,0,218,63896,-1,0],
["E6627D73DB5C05D4049221106A381B602DAA0C25",201,"Dispatches.How.To.Save.100.Million.WS.PDTV.XviD-FTP",374220400,1000,374220400,1146880,3,0,0,-1,"",0,0,0,0,65536,-1,0],
["A09EDC6EA23C6AFCE2E77F39B478BEAAE81899CC",201,"Doctor.Who.2005.S05E11.HDTV.XviD-BiA",378721892,1000,378721892,169230336,446,0,0,-1,"",0,0,0,0,65536,-1,0],
["B4CC375419F6C5B4BBFDD5882F30B9643D69A4AC",152,"Doctor_Who_2005.5x10.Vincent_And_The_Doctor.HDTV_XviD-FoV",379578689,1000,379578689,86157633,226,0,0,0,"",0,0,0,0,65536,-1,0],
["8C8E3DC516F4D05F140F3700BD6C523511D5249A",201,"Family.Guy.S08E21.PDTV.XviD-BiA",207562445,1000,182976512,35323904,193,22,0,-1,"",1,104,0,422,57757,-1,0],
["9EEF7F6BC11D2BB06D5E24E6A01D0EB17ECA3B6E",152,"Hot.Tub.Time.Machine.2010.R5.LiNE.XviD-Rx",1508663769,1000,1493172224,24100864,16,0,0,0,"",0,0,0,0,64852,-1,0],
["810BCA5519C10D0DD479C63B15C9815EDEE579E5",201,"Stargate.Universe.S01E20.Incursion.Part.2.HDTV.XviD-FQM",375323574,1000,367721398,52412416,142,11,2,-1,"",0,53,0,249,64208,-1,0],
["5573EC5459547E69599BDE4DFB3A1B2D3DCF7CE1",201,"Super.Mario.Galaxy.2.PAL.Wii-LoCAL",4698223194,1000,4677759578,1685697114,360,0,0,-1,"",0,2,0,0,65536,-1,0],
["7BE7BD3CD47886308A1AABF0BD6A51142DF0347C",201,"Through.the.Wormhole.S01E02.The.Riddle.of.Black.Holes.HDTV.XviD-MOMENTUM",377353721,1000,367001600,409600,1,228,0,2412687,"",1,10,0,47,67629,-1,0]]
,"torrentc": "1360124475"}

Im just wondering if it could be something to do with my lacklustre attempt at the regex.

I'd be delighted if a keen eye could give me hint at my problem.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Try this:

Local $s = FileRead("json.txt")
Local $temp = StringRegExp($s, '(?m)^\[([^\]]*)\](?>[,\]]$)', 3)
_ArrayDisplay($temp)

Here, json.txt is a copy/paste of your example, so $s is identical to your BinaryToString(InetRead($sList_URL, 1))

It may not be 100% failproof as it will break if ever the line format changes, but that may be unlikely to occur spontaneously.

BTW, you shouldn't use function pipelining, as it gains very little and makes you vulnerable to error(s), should some inner function fail.

While it's fairly common to pipeline functions that aren't subject to failure, things like InetRead _are_ open to errors and should have a statement fully devoted for them.

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

  • Recently Browsing   0 members

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