Jump to content

Read File With StringRegExp


jfcby
 Share

Recommended Posts

Hi,

I'm trying to get the following script to read the sections in my text below but it will not read my file.

Text File:

<Title> 
Acceptance Or Denial: A Process Making Progress With A Planned Outline Overview
</Title>

  
<Summary>
It is time to get down to the nitty gritty of things and begin to work out a solution to the problems, issues, situations, positions, conditions, circumstances or affairs that are encountered in life everyday. These factors will help us to accept the fact that changes will need to be made in our life to improve our mental knowledge, physical condition, and well being.
</Summary>

 
<Body>
It is time to get down to the nitty gritty of things and begin to work out a solution to the problems, issues, situations, positions, conditions, circumstances or affairs that are encountered in life everyday. These factors will help us to accept the fact that changes will need to be made in our life to improve our mental knowledge, physical condition, and well being.
 
Our main objective is to plan, prepare, and process an outline to get visible results that are so desperately wanted. Accepting the challenges of life 
will help us to improve, build upon, and strengthen us to be able to face our worst enemies. An essential part of this technique is to hone your troubleshooting skills and abilities.
 
This will allow you to be able to make achievements that would ordinarily be impossible. But with a good solid process that outlines a starting point to work from to find a solution will suffice. This plan will need to be simple, easy to understand, and easy to use because most of the time you will be stress out. A lot will be going through your mind and your body will be emotionally reacting to them.
 
A good place to start would be to take the following steps...
 
First step-- you would need a general overview of the different directions that could be taken.
Second step-- adding a general description to each would be extremely useful. 
Third step-- outline the directions mapping out the general detailed contents of what you can use and how to use them.
</Body>

Code:

#include <Array.au3>

$file = FileOpen(@ScriptDir & "\TextFilesExamples\New1.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;Option 1, using offset
$nOffset = 1
While 1
    $array = StringRegExp(FileReadLine($file), '<(?i)Title>(.*?)</(?i)Title>', 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test 1 - " & $i, $array[$i])
    Next
WEnd


FileClose($file)

Thank you for your help,

jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

This might help.

Local $sFileName = "New1.txt"
Local $sFileContents = FileRead($sFileName)
If FileExists($sFileName) And (StringInStr($sFileContents, "<Title>") <> 0) And (StringInStr($sFileContents, "</Title>") <> 0) Then
    Local $sTitle = StringRegExpReplace($sFileContents, '(?i)(?s).*?<Title>\s*(.*?)\s*</Title>.*', "\1")
    MsgBox(0, 'Title from "' & $sFileName & '" file', $sTitle)
EndIf

Edit: Added two StringInStr() conditions in the "If" statement.

Edited by Malkey
Link to comment
Share on other sites

Here you go.

testfun()

Func testfun()
    Local $array,$Offset,$numloop,$EntireFile
    $EntireFile = FileRead(@ScriptDir & "\TextFilesExamples\New1.txt")

    While True

        $numloop += 1
        $array = StringRegExp($EntireFile, "\n\<(.*[^\<\>]\>)([^\<\>]*)", 1, $Offset)
        If @error Then ExitLoop
        $Offset = @extended
        If IsArray($array) Then
            For $n = 0 To UBound($array) - 1
                ConsoleWrite('loop=' & $numloop & ' $array[' & $n & '] element=' & $array[$n] & @CRLF)
            Next
        EndIf
    WEnd
EndFunc

Console Output of above

loop=1 $array[0] element=Title>

loop=1 $array[1] element=

Acceptance Or Denial: A Process Making Progress With A Planned Outline Overview

loop=2 $array[0] element=Summary>

loop=2 $array[1] element=

It is time to get down to the nitty gritty of things and begin to work out a solution to the problems, issues, situations, positions, conditions, circumstances or affairs that are encountered in life everyday. These factors will help us to accept the fact that changes will need to be made in our life to improve our mental knowledge, physical condition, and well being.

loop=3 $array[0] element=Body>

loop=3 $array[1] element=

It is time to get down to the nitty gritty of things and begin to work out a solution to the problems, issues, situations, positions, conditions, circumstances or affairs that are encountered in life everyday.

etc.......

Edited to add this regexp line

replace StringRegExp above with

$array = StringRegExp($EntireFile, "\n<.*[^<>]>\r\n([^<>]*)", 1, $Offset)

then output looks like

loop=1 $array[0] element=Acceptance Or Denial: A Process Making Progress With A Planned Outline Overview

loop=2 $array[0] element=It is time to get down to the nitty gritty of things and begin to work out a solution to the problems .............

Edited by Quual
Link to comment
Share on other sites

If you wish to to collect all the sections, try this.

In the replace parameter of the StringRegExpReplace command, "\1" is the back-reference for the section name. "\2" is the back-reference for the section contents.

If only the section contents are required then change the replace parameter with this "\2" & @CRLF & @CRLF

i.e. use

Local $sTitle = StringRegExpReplace($sFileContents, '(?i)(?s).*?<(.+?)>\s*(.*?)\s*</\1>.*?', "\2" & @CRLF & @CRLF)

local $sFileName = "New1.txt"
local $sFileContents = FileRead($sFileName)
If FileExists($sFileName) and (StringInStr($sFileContents,"<Title>") <> 0) and (StringInStr($sFileContents,"</Title>") <> 0) Then
    Local $sTitle = StringRegExpReplace($sFileContents, '(?i)(?s).*?<(.+?)>\s*(.*?)\s*</\1>.*?', '"\1" is - \2' & @CRLF & @CRLF)
    MsgBox(0, 'Title from "' & $sFileName & '" file', $sTitle)
EndIf
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...