Jump to content

Regex or Some other way


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

@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

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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

Link to comment
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

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