Jump to content
Sign in to follow this  
Renw

Text Parsing

Recommended Posts

Renw

Hi,

I'm trying to write a script that parses a text file and writes, in an output text file, every word between determinate characters.

e.g.

I have (asfas)....(hths)....(4363)...(snbsns).

As result i want a text file with a list of words between '(' and ')'

I'm using FileOpen to open .txt file.

_StringBetween ($FileOpened, $Start, $End).

_ArrayToString.

FileWrite.

The only thing I get in the output file is "-1"

How can I do this properly?

(Words are different by lenght and by position)

Thanks.

Share this post


Link to post
Share on other sites
Chimp

I think you missed FileRead() between FileOpen and _StringBetween


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Renw

Thanks for your answer. I tried, still get -1.

Share this post


Link to post
Share on other sites
mikell

?

#Include <Array.au3>

$str = "this(asfas)..is..(hths)..a..(4363).text..(snbsns)end."
$res = StringRegExp($str, '\(([^)]+)', 3)
 _ArrayDisplay($res)

Edit

$str = "this(asfas)..is..(hths)..a..(4363).text..(snbsns)end."
$res = StringStripWS(StringRegExpReplace($str, '(?x)(  (?:^|\)) [^(]* (?:\(|$)  )', @crlf), 3)
Msgbox(0,"", $res)

:)

Edited by mikell

Share this post


Link to post
Share on other sites
Renw

Thanks for your answer, now I've gotta go, later I will try. 

Anyway, my code so far:

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


Local Const $sFileOpen = FileOpen(@DesktopDir &"\source002.txt")
Local Const $sFile = FileRead("$sFileOpen")
Local Const $Start = '} ">'
Local Const $End = '<'
Local $aRet = _StringBetween($sFile, $Start, $End)
Local $sStringRet = _ArrayToString($aRet, @CRLF)
FileWrite(@DesktopDir &"\aaaa.txt", $sStringRet)
FileClose($sFileOpen)

(In _StringBetwen I've tried both $File and $sFileOpen)

Sorry for noobness.

Thanks.

Share this post


Link to post
Share on other sites
mikell

For use with different couples of chars a loop is needed

#Include <Array.au3>

$str = "this(asfas)..is..{hths}..a..(4363).text..<snbsns>end."

Local $c[3][2] = [["(", ")"], ["{", "}"], ["<", ">"]]   ; marks
; _ArrayDisplay($c)

Local $final = ""
For $i = 0 to 2
  $tmp = StringStripWS(StringRegExpReplace($str, '((?:^|\' & $c[$i][1] & ')[^' & $c[$i][0] & ']*(?:\' & $c[$i][0] & '|$))', @crlf), 1)
  $final &= $tmp
Next
Msgbox(0,"", $final)

But if the initial text is complicated it's worth to make several steps in the code

Share this post


Link to post
Share on other sites
Renw

There is no $File

However this

FileRead("$sFileOpen")

Should be

FileRead($sFileOpen)

Oh, that's it. Got it working, thanks.

Also thank you mikell.

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  

×