Renw Posted January 11, 2015 Posted January 11, 2015 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.
Gianni Posted January 11, 2015 Posted January 11, 2015 I think you missed FileRead() between FileOpen and _StringBetween Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
Renw Posted January 11, 2015 Author Posted January 11, 2015 Thanks for your answer. I tried, still get -1.
JohnOne Posted January 11, 2015 Posted January 11, 2015 Show your actual code. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
mikell Posted January 11, 2015 Posted January 11, 2015 (edited) ? #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 January 11, 2015 by mikell
Renw Posted January 11, 2015 Author Posted January 11, 2015 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.
JohnOne Posted January 11, 2015 Posted January 11, 2015 There is no $File However this FileRead("$sFileOpen") Should be FileRead($sFileOpen) AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
mikell Posted January 11, 2015 Posted January 11, 2015 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
Renw Posted January 12, 2015 Author Posted January 12, 2015 There is no $File However this FileRead("$sFileOpen") Should be FileRead($sFileOpen) Oh, that's it. Got it working, thanks. Also thank you mikell.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now