Jump to content

Parsing/Extrating String Text From a .Txt file.


Go to solution Solved by Malkey,

Recommended Posts

I have a group of text files that I need to search through containing multiple of these strings.

<a href="http://Website.com/profiles/76561198034531966">

What i need to do is pull random Numbers. eg. 76561198034531966 or the entire string.

is there anyway i can do this? if so can someone help me Code this?

I have a Basic knowledge in using autoit

Thanks in Advance,

Edited by Siahtech
Link to comment
Share on other sites

StringMid will help if <a href="http://Website.com/profiles/ is a constant

if 76561198034531966 is a constant no. of digits you can use StringRight

you can pull out the href http://Website.com/profiles/76561198034531966 using a _StringBetween

Otherwise you would have to use StringRegExp.

Try first and post what you have tried so far.

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

My code so far

I cant get it to loop, and I want it to output to a txt file.

$file = "a.txt"
FileOpen($file, 0)
$String = FileReadLine($file)
$Start = 'Website.com/profiles/'
$End = '">'

Func _StringBetween($String, $Start, $End = 0)
    $Start = StringInStr($String, $Start)+StringLen($Start)
    return StringMid($String, $Start, StringInStr($String, $End)-$Start)
 EndFunc


    MsgBox(0, "Test", _StringBetween($String, $Start, $End))
Link to comment
Share on other sites

Use this instead

#include <String.au3>

$String = '<a href="http://Website.com/profiles/76561198034531966">'
$Start = 'Website.com/profiles/'
$End = '">'

;im talking about the function that comes with the native Autoit Library
;it returns a 0-based array
$aRet = _StringBetween($String, $Start, $End)
MsgBox(0, "Test", $aRet[0])

Edit : Have you checked the Helpfile ?

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Solution

My code so far

I cant get it to loop, and I want it to output to a txt file.

$file = "a.txt"
FileOpen($file, 0)
$String = FileReadLine($file)
$Start = 'Website.com/profiles/'
$End = '">'

Func _StringBetween($String, $Start, $End = 0)
    $Start = StringInStr($String, $Start)+StringLen($Start)
    return StringMid($String, $Start, StringInStr($String, $End)-$Start)
 EndFunc


    MsgBox(0, "Test", _StringBetween($String, $Start, $End))

 

Here is one method of looping through the file and using your _StringBetween function.

Local $fileIn = "a.txt"
#cs a.txt file contains:-
<a href="http://Website.com/profiles/76561198034531966">
<a href="http://Website.com/profiles/98656119802">
#ce

Local $sTemp, $sStringRet = ""
Local $Start = 'Website.com/profiles/'
Local $End = '">'
Local $file = FileOpen($fileIn, 0)
While 1
    $sTemp = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $sStringRet &= _StringBetween($sTemp, $Start, $End) & @CRLF
WEnd
FileClose($file)

MsgBox(0, "Test", $sStringRet)


Func _StringBetween($String, $Start, $End = 0)
    $Start = StringInStr($String, $Start) + StringLen($Start)
    Return StringMid($String, $Start, StringInStr($String, $End) - $Start)
EndFunc   ;==>_StringBetween
 

 

This example is using the _StringBetween function found in the ..AutoIt3IncludeString.au3 file that returns an array.

#include <String.au3>
#include <Array.au3>

Local $String = '<a href="http://Website.com/profiles/76561198034531966">' & @CRLF & _
        '<a href="http://Website.com/profiles/98656119802">'
Local $Start = 'Website.com/profiles/'
Local $End = '">'

Local $aRet = _StringBetween($String, $Start, $End)
;_ArrayDisplay($aRet)
Local $sStringRet = _ArrayToString($aRet, @CRLF)
MsgBox(0, "Substrings", $sStringRet)
 

 

Unlike the other two examples, this example does not rely on all web addresses starting with "http://Website.com/profiles/".

Local $fileIn = "a.txt"
Local $fileOut = "aOut.txt"
Local $file = FileOpen($fileIn, 0)

;Local $String = FileRead($file)
;Or
;#cs  ; Test Data
Local $String = '<a href="http://Website.com/profiles/76561198034531966">' & @CRLF & _
        '<a href="http://AnotherWebsite.com/files/98656119802">'
;#ce

$sStringMod = StringRegExpReplace($String, '(?i).+"http:.+/(\d+)".+', "\1") ; Case-insensitive

;FileWrite($fileOut,$sStringMod) ; Append $sStringMod data to $fileOut file.

MsgBox(0, "Substrings", $sStringMod)
Link to comment
Share on other sites

A similar way, using an array

; #include <Array.au3>   ; for _ArrayDisplay
#include <File.au3>   ; for _FileWriteFromArray

; Local $String = FileRead("txt_in.txt")
Local $String = 'text <a href="http://Website.com/profiles/76561198034531966">' & @CRLF & _
            '<a href="http://AnotherWebsite.com/files/98656119802"> text 2'  & _ 
            'other text<a href="http://Website21.com/profiles12/113456789123456">'

$aRet = StringRegExp($String, '(?s)href=".+?(\d+)"', 3)  ; array of matches

; _ArrayDisplay($aRet)   ; preview
$file = FileOpen("txt_out.txt", 1)  ; open for writing
_FileWriteFromArray($file, $aRet)
FileClose($file)
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...