Jump to content

Text Parsing


Renw
 Share

Recommended Posts

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.

Link to comment
Share on other sites

?

#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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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...