Sign in to follow this  
Followers 0
xcaliber13

Regex or Some other way

5 posts in this topic

Hey Everyone,

        Can you get me going in the right direction please.  Here is what I am trying to do:

I have a large text file that I want to find a string.    <--  No problem with that part

Once I find that string  which is always at the beginning of the line     Example string     ABC:

After the found string (ABC:) there are  sets of 5 characters/Numbers separated by commas.  Example:   J0203, 77955

So the line looks like this:     ABC:  G0203, 77955

Some lines have just one set with no trailing comma and some have more than one set.   I want to count the sets into a variable.

Would I do this with StringRegExp  or some other way?

Thank you

Share this post


Link to post
Share on other sites

@xcaliber13 something like this perhaps?

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $aText = FileReadToArray(@DesktopDir & "\Test1.txt")
    For $i = 0 To UBound($aText) - 1
        $aTemp = StringSplit($aText[$i], ",")
        MsgBox($MB_OK, "", "Found " & $aTemp[0] & " data sets in line " & $i + 1)
    Next

Works on a text file like this:

ABC: G0203, 77955
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 
ABC: G0203, 77955, 42358, 65989, 12115
ABC: G0203, 77955, 42358, 65989
ABC: G0203, 77955, 42358, 65989, 12115

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Not sure I understood, maybe this ?

#Include <Array.au3>

$string = "ABC:"
Local $sText = FileRead(@DesktopDir & "\Test1.txt")
$a = StringRegExp($sText, '(?m)(?<=^' & $string & '\h|\w{5},\h)\h*(\w{5})', 3)
;_ArrayDisplay($a)
Msgbox(0,"", UBound($a)  & " sets in all the lines " & $string)

 

Edited by mikell

Share this post


Link to post
Share on other sites

xcaliber13,

Another way (similar to JL's)...

#Include <Array.au3>

local $aText = stringregexp(fileread(@DesktopDir & "\Test.txt"),'.*\R',3)
local $aFinal[ubound($aText)][2]

for $1 = 0 to ubound($aText) - 1
    StringRegExpReplace($aText[$1],'\w{5}','')
    $aFinal[$1][0] = @extended
    $aFinal[$1][1] = $aText[$1]
Next

_arraydisplay($aFinal)

For a definitive answer you should post real data.  It is unlikely that the sample we're using is representative.  Also, as you can see, JL and I interpreted your task one way and mikell another.  Please define your requirements further. 

kylomas

 

 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

JLogan3o13, MikeII, kylomas,

         Thank you all for the replies.  Between all of your examples I was able to do what I need to.  Get the total count of all the sets. Just another example why AutoIt is so powerful.  The Autoit forums are the best there is as far as actually helping with coding problems.   You guys are the best.

Again thank you to all

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  
Followers 0