Jump to content

Help with StringRegExp


Recommended Posts

Have a piece of code that is supposed to check a text file for every line that ends in "001". It should take those lines and write them to a new file. So I have a text file that reads:

07566276.001

07566276.002

07566276.003

07566276.004

07566276.005

07566276.006

07566307.001

07566307.002

07566307.003

07566307.004

07566307.005

07566311.001

07566311.002

07566311.003

07566311.004

07566311.005

The new file ($file2) should read:

7566276

7566307

7566311

If I take out the StringRegExp the code works. It just writes and trims the whole list, so I know It is something with that statement. What am I doing wrong.

$file = FileOpen("C:\Corrected_Scans\Before\cslist.txt", 0)
$file2 = FileOpen("C:\Corrected_Scans\cslist2.txt", 1)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
    $chars = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $chars = StringRegExp($chars, ".........001")
    If @error = 1 then
    $chars = StringTrimLeft($Chars, 1)
                $chars = StringTrimRight($Chars, 4)
    $chars = StringStripCR($Chars)
    $chars = stringstripws($chars, 2)
                filewriteline ($file2, $chars)
    endif
Wend
FileClose($file)
FileClose ($file2)
Link to comment
Share on other sites

  • Moderators

I have no idea what you are really asking... but if you know that StringRegExp() returns a 0 base array.... this should work from how I am interpreting it.

#include <array.au3>
$sRead = FileRead(@HomeDrive & '\Corrected Scans\cslist2.txt')
$a001 = StringRegExp($sRead, '(?s)(?i)0(\d+)\.001', 3)
_ArrayDisplay($a001)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

SmOke_N,

Sure... give the OP what he asked for.... :-)

;opens file and splits it into an array
$FileArray = StringSplit(FileRead("cslist.txt"), @CRLF)

;opens output file in the append mode
$file2 = FileOpen("cslist2.txt", 1)
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;loops thru each array element looking for .001
For $i = 1 To $FileArray[0]
    If StringMid($FileArray[$i], 9, 4) = ".001" Then FileWriteLine($file2, StringLeft($FileArray[$i], 8))
Next
FileClose($file2)
Edit: It worked for me when I tested it.

Watch the single line IF statement as it may wrap in your forum display.

Edit2: changed/tested code to look for ".001"

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

dont know if this helps, but...

$Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF)

For $x = 1 to $Lines[0]-1

$Split=StringSplit($Lines[$x],".")

if IsArray($Split) And $Split[2] = "001" then FileWrite("File2.txt",$Split[1] & @CRLF)

Next

Edited by Rick

Who needs puzzles when we have AutoIt!!

Link to comment
Share on other sites

  • Moderators

Think you two forgot about the leading zero :rolleyes::rambo:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

oops

$Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF)

For $x = 1 to $Lines[0]-1

$Split=StringSplit($Lines[$x],".")

if IsArray($Split) AND $Split[2] = "001" then FileWrite("File2.txt",StringTrimLeft($Split[1],1) & @CRLF)

Next

Edited by Rick

Who needs puzzles when we have AutoIt!!

Link to comment
Share on other sites

Think you two forgot about the leading zero :rolleyes::rambo:

Like I said - you keep given the OP what they ask for.

;opens file and splits it into an array
$FileArray = StringSplit(FileRead("cslist.txt"), @CRLF)

;opens output file in the append mode
$file2 = FileOpen("cslist2.txt", 1)
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;loops thru each array element looking for .001
For $i = 1 To $FileArray[0]
    If StringMid($FileArray[$i], 9, 4) = ".001" Then
        If StringLeft($FileArray[$i], 1) = "0" Then
            FileWriteLine($file2, StringMid($FileArray[$i], 2, 7))
        Else
            FileWriteLine($file2, StringLeft($FileArray[$i], 8))
        EndIf
    EndIf
Next
FileClose($file2)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

oops

$Lines=StringSplit(StringReplace(FileRead("file1.txt"),@CR,""),@LF)

For $x = 1 to $Lines[0]-1

$Split=StringSplit($Lines[$x],".")

if IsArray($Split) AND $Split[2] = "001" then FileWrite("File2.txt",StringTrimLeft($Split[1],1) & @CRLF)

Next

Hey Rick,

This code returns an error that says "Array Variable has incorrect number of subscripts or subscript dimension range exceeded."

I have no idea what that means. Here is what I tried to run:

$Lines=StringSplit(StringReplace(FileRead("C:\Corrected_Scans\Before\cslist.txt"),@CR,""),@LF)
For $x = 1 to $Lines[0]-1
$Split=StringSplit($Lines[$x],".")
if IsArray($Split) AND $Split[2] = "001" then FileWrite("C:\Corrected_Scans\cslist2.txt",StringTrimLeft($Split[1],1) & @CRLF)
Next
Link to comment
Share on other sites

Hey Rick,

This code returns an error that says "Array Variable has incorrect number of subscripts or subscript dimension range exceeded."

erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??!

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Like I said - you keep given the OP what they ask for.

;opens file and splits it into an array
$FileArray = StringSplit(FileRead("cslist.txt"), @CRLF)

;opens output file in the append mode
$file2 = FileOpen("cslist2.txt", 1)
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;loops thru each array element looking for .001
For $i = 1 To $FileArray[0]
    If StringMid($FileArray[$i], 9, 4) = ".001" Then
        If StringLeft($FileArray[$i], 1) = "0" Then
            FileWriteLine($file2, StringMid($FileArray[$i], 2, 7))
        Else
            FileWriteLine($file2, StringLeft($FileArray[$i], 8))
        EndIf
    EndIf
Next
FileClose($file2)
Right on. This works perfecty. The only thing I really don't understand is the lines:

For $i = 1 To $FileArray[0]

If StringMid($FileArray[$i], 9, 4) = ".001" Then

Any chance you can explain them to me a little or point me to a reference. I have read the docs section on arrays, but am still struggling to uderstand that concept. Variables I get, arrays I don't. By the way, what is an OP? Thanks.

Link to comment
Share on other sites

  • Moderators

Like I said - you keep given the OP what they ask for.

;opens file and splits it into an array
$FileArray = StringSplit(FileRead("cslist.txt"), @CRLF)

;opens output file in the append mode
$file2 = FileOpen("cslist2.txt", 1)
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;loops thru each array element looking for .001
For $i = 1 To $FileArray[0]
    If StringMid($FileArray[$i], 9, 4) = ".001" Then
        If StringLeft($FileArray[$i], 1) = "0" Then
            FileWriteLine($file2, StringMid($FileArray[$i], 2, 7))
        Else
            FileWriteLine($file2, StringLeft($FileArray[$i], 8))
        EndIf
    EndIf
Next
FileClose($file2)
:rolleyes:
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??!

Yes it is. Just thought you would like to know. Besides, 2 solutions are better than one. I learn more too :rolleyes:
Link to comment
Share on other sites

  • Moderators

erm... errors??? Why don't you just run the code of SmOke_N. It's the perfect solution to your problem !??!

Actually, reading through the rest of the posts got me thinking that mine wasn't the "best" solution.... I don't like the "0" leading digits thing with no minimum on the characters other than "1".

I think this is smarter if you know that there will always be 7 digits before the decimal and after the leading zero....

#include <array.au3>
$sRead = FileRead(@HomeDrive & '\Corrected Scans\cslist2.txt')
$a001 = StringRegExp($sRead, '(?s)(?i)0(\d{7})\.001', 3)
_ArrayDisplay($a001)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Right on. This works perfecty. The only thing I really don't understand is the lines:

For $i = 1 To $FileArray[0]

If StringMid($FileArray[$i], 9, 4) = ".001" Then

Any chance you can explain them to me a little or point me to a reference. I have read the docs section on arrays, but am still struggling to uderstand that concept. Variables I get, arrays I don't. By the way, what is an OP? Thanks.

You are the OP. :-)

OP = the person who made the Original Post or the Original Post itself - depending on how it is used.

When you use StingSplit, it puts the info into an Array like this:

$FileArray[0] = 16 <--- this will be how many numbers are in your input file

$FileArray[1] = 07566276.001

$FileArray[2] = 07566276.002

----------

EDIT:

So For $i = 1 To $FileArray[0]

becomes For $i = 1 To 16

Each time thru the loop, $i increments by 1 (by default)

This allows you to work thru an array with ease.

----------

The first time thru the loop, this line:

StringMid($FileArray[$i], 9, 4)

will be:

StringMid($FileArray[1], 9, 4)

It [StringMid] is reading this number...

07566276.001

123456789

starting at the 9th position [which is the decimal]

and returning 4 characters [.001]

so, StringMid($FileArray[1], 9, 4) = ".001"

becomes

".001" = ".001"

and If ".001" = ".001" then write that line to the file*

*or you might want to trim the leading 0

hope this helps

Edit2: [in bold]

Edit3: hope that was not too insulting...

I was unsure about what you were unsure about.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

By the way, what is an OP?

try this:

#include <file.au3>

$file_in = @HomeDrive & '\Corrected Scans\cslist2.txt'
$file_out = @HomeDrive & '\Corrected Scans\cslist2_out.txt'

$sRead = FileRead($file_in)
$a001 = StringRegExp($sRead, '(?s)(?i)0(\d+)\.001', 3)
_FileWriteFromArray($file_out,$a001)

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Actually, reading through the rest of the posts got me thinking that mine wasn't the "best" solution.... I don't like the "0" leading digits thing with no minimum on the characters other than "1".

with the information given, it was the best assumption ;-)

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

actually this pattern is more appropriate: http://www.autoitscript.com/forum/index.ph...st&p=356431

EDIT: I don't know where the strange characters in my reply come from. Seems to be a forum bug...

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

testing reply

please reply to this post: http://www.autoitscript.com/forum/index.ph...st&p=356501

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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