Jump to content

String Between


Clay
 Share

Recommended Posts

Ok here is the scoop. I have a text file which I am reading and I want to extract some data from it... the text file looks like the following

<?xml version='1.0' standalone='yes'?><response><package_ids><package_id>23147</package_id><package_id>23136</package_id><package_id>23133</package_id>
<package_id>23123</package_id><package_id>23099</package_id><package_id>23091</package_id><package_id>23054</package_id><package_id>23051</package_id>
<package_id>23048</package_id><package_id>23036</package_id></package_ids><error><code>0</code><message></message></error></response>

I want to extract all the series of 5 digit numbers within this long string such as 23147, 23136 and output it to another text file or array.

I am attempting tp use StringBetween to see if I can get at least one of the 5 digit strings and out put it... I wrote up this piece of code quickly to see if I could accomplish this but can't seem to get the results I want .....I get a blank message box actually.

This is the code

#include <String.au3>

Dim $Result 
Dim $filesample 
Dim $Check

$filesample = FileOpen("sample.txt", 0)
If $filesample = -1 Then
    MsgBox(0, "Error", "Unable to open Exclusion File, please verify that file exists.")
    Exit
EndIf

$Result =  FileReadLine($filesample)
$Check = _StringBetween( $Result, '<package_id>', '</package_id>')
MsgBox(0,"DevCentral Result equals: ", $Check)

Does anyone know why this does not work ? Is there a better or more efficent way to extract at least the first 5 digits and more importantly all the 5 digit combination in the string?

p.s./ While I know my $check variable will only hold one of the strings if any at all .. I was just attempting a qiuck and dirty go at it to see what would be returned?

Any help would be grealy appreciated.

Link to comment
Share on other sites

_StringBetween returns an array...

#include <Array.au3>

_ArrayDisplay($Check)

Also you wouldn't want to read the file using FileReadLine, you will miss data that way. Just use FileRead since you are trying to find all matches.

Link to comment
Share on other sites

  • Moderators

_StringBetween returns an array, you are not checking it as an array.

#include <array.au3>
$Check = _StringBetween( $Result, '<package_id>', '</package_id>')
_ArrayDisplay($Check)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

_StringBetween returns an array, you are not checking it as an array.

#include <array.au3>
 $Check = _StringBetween( $Result, '<package_id>', '</package_id>')
 _ArrayDisplay($Check)
Is there an echo in here? :D
Link to comment
Share on other sites

This works using your example txt file

#include <String.au3>
#include <file.au3>

Dim $Result 
Dim $filesample 
local $Rtn = ""
Local $fArray

If NOT FileExists("sample.txt") Then
    MsgBox(0, "Error", "Unable to open Exclusion File" & @CRLF & "It appears that the file does not exist.")
    Exit
Else
   _FileReadToArray("sample.txt", $fArray)
   
   For $I = 1 to Ubound($fArray)-1
      $Check = _StringBetween( $fArray[$I], '<package_id>', '</package_id>')
      $Rtn &= $Check[0] & @CRLF
   Next
EndIf

MsgBox(0,"DevCentral Result equals: ", $Rtn)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Quick and dirty (no errorchecking and assuming numbers are allways of length 5)

Search also forum for XML as there is also an XML UDF library

$XmlString=fileread("sample.txt")

$matchI=1
$pos=stringinstr($xmlString,"<package_id>",2,$matchI)

while ($pos > 0)
    consolewrite(stringmid($xmlString,$pos+12,5) & @crlf)               ;12 is length of <package_id>
    $matchi+=1
    $pos=stringinstr($xmlString,"<package_id>",2,$matchi)
WEnd
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...