Jump to content

StringRegExp help


Recommended Posts

My major weakness in Autoit is StringRegExp... I have been practicing with it a bit and Simucal has been helping me and such... But I am still hopeless. I need a StringRegExp pattern to get the dates in these strings in the YYYY/MM/DD HH:MM format so that I can do a dateadd. Here is a sample of the strings I will need this to work on (It only needs to do it one line at a time).

BananaCake Jul 16, '06 07:16 Civilian

SpamOnRye Jul 16, '06 07:47 Civilian

Jell-O Jul 16, '06 11:03 Civilian

PuddingPop Jul 16, '06 13:23 Civilian

TheMunchies Jul 16, '06 13:57 Civilian

StrawberrySmoothie Jul 16, '06 17:54 Civilian

Spaghetti_Os Jul 16, '06 21:57 Civilian

Mini_Ravioli Jul 16, '06 23:57 Civilian

yem Jul 17, '06 13:42 Civilian

BlueMurder Jul 18, '06 12:34 Thug

WickedMind Jul 18, '06 19:19 Civilian

Also, there is an unknown number of spaces in between the name and the date part... Autoit won't let me have a long line of spaces though apparently.

Bridget_Currio Jul 19, '06 00:03 Thug

Iron_man Jul 19, '06 15:40 Civilian

The_Don Jul 19, '06 22:43 Civilian

Edited by brodie28
Link to comment
Share on other sites

  • Moderators

Try this:

Local $sFilePath = @ScriptDir & '\locals.txt'
Local $Months[12] = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], $AllDates = ''
For $iCount = 0 To 11
    $aArray = _SRE_Between($sFilePath, $Months[$iCount], ", '(\d{2})")
    If IsArray($aArray) Then
        For $xCount = 0 To UBound($aArray) - 1 Step 2
            $AllDates &= $Months[$iCount] & $aArray[$xCount] & ", '" & $aArray[$xCount + 1] & @CRLF
        Next
    EndIf
Next

MsgBox(64, 'Info:', 'All Dates:' & @CR & StringTrimRight($AllDates, 2))

Func _SRE_Between($s_FilePath, $s_Start, $s_End)
    $h_FRead = FileRead($s_FilePath, FileGetSize($s_FilePath))
    $a_Array = StringRegExp($h_FRead, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3)
    If Not @error Then Return $a_Array
EndFunc

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

I just need it to do one at a time... SO don't worry about all the other stuff...

I just need some code where any one of those lines can be put in, and it will return the date in the correct format.

That code did not seem to work.

Link to comment
Share on other sites

  • Moderators

I just need it to do one at a time... SO don't worry about all the other stuff...

I just need some code where any one of those lines can be put in, and it will return the date in the correct format.

That code did not seem to work.

Then try to modify it, or upload the text file because I used what you've provided and obviously tested it before I just decided to paste it here, it only does it one time (each time you run that particular script).

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

The text file is basically what I included in my first post, except with a random number of spaces between the name and the date section. Here is the function it is going into and what it looks like so far.

Func dateadd()
$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen ( "locals.txt", 0)

    For $number = 1 to $nolines
    $line =  FileReadLine($locals, $number)
    
;something to go here that takes $line and turns it into a YY/MM/DD HH:MM date
    Msgbox(0, "date", $date)
    
    next


    
EndFunc
Link to comment
Share on other sites

  • Moderators

The text file is basically what I included in my first post, except with a random number of spaces between the name and the date section. Here is the function it is going into and what it looks like so far.

Func dateadd()
$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen ( "locals.txt", 0)

    For $number = 1 to $nolines
    $line =  FileReadLine($locals, $number)
    
;something to go here that takes $line and turns it into a YY/MM/DD HH:MM date
    Msgbox(0, "date", $date)
    
    next
    
EndFunc
:wacko::D GL

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

  • Moderators

Sigh.

Thanks anyway.

Ok, listen closely, because I will only do this once. This is kind of long because of the Months... I have to go through all 12 months to make sure we are getting the right string. I've taken all of the white spaces out of the equation that would cause issues with StringStripWS('whatever', 7).... There's one more thing I could try to avoid the Months... but this is tested (with different spaces) and works.
Local $Months[12] = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], $AllDates = ''
Local $sFileRead = FileRead($sFilePath)

For $iCount = 0 To 11
    $aArray = StringRegExp(StringStripWS($sFileRead, 7), "(?:" & $Months[$iCount] & ")(\D{0}\d{2}\D{0}\d{2}\D{0}\d{2}\D{0}\d{2})", 3)
    If IsArray($aArray) Then
        For $xCount = 0 To UBound($aArray) - 1
            $AllDates &= $Months[$iCount] & $aArray[$xCount] & @CRLF
        Next
    EndIf
Next
$AllDates = StringTrimRight($AllDates, 2)

FileWrite(@ScriptDir & '\localsDates.txt', $AllDates)
Run('notepad.exe "' & @ScriptDir & '\localsDates.txt"')oÝ÷ Øù^jÇ©k&­µéíí«HÁ«'ßÛek
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

Hi @Smok_In

I had a problem with your first script, too, with the quotes.

I modified it a little; this works for me;

;tryt.au3
Local $sFilePath = @ScriptDir & '\try.txt'
Local $Months[12] = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], $AllDates = '', $AllDates2 = ''
For $iCount = 0 To 11
    $aArray = _SRE_Between($sFilePath, $Months[$iCount], ", '(\d{2})(?: )(\d{1,2})(?::)(\d{1,2})")
    If IsArray($aArray) Then
        For $xCount = 0 To UBound($aArray) - 1 Step 4
            $AllDates2 &= "20" & $aArray[$xCount + 1]&"/" &StringRight("00"&$iCount+1,2)&"/" &  StringRight("00"&$aArray[$xCount],2)&" " &StringRight("00"&$iCount+2,2)&":" &  StringRight("00"&$aArray[$xCount+3],2)  & @CRLF
        Next
    EndIf
Next
MsgBox(64, 'Info:', 'All Dates2:' & @CR & StringTrimRight($AllDates2, 2))

Func _SRE_Between($s_FilePath, $s_Start, $s_End)
    $file = FileRead($s_FilePath)
    $a_Array = StringRegExp($file, '(?:' & $s_Start & ' )(.*?)(?:' & $s_End & ')', 3)
    If Not @error Then Return $a_Array
EndFunc
Best, randallc

All Dates2:

2006/07/16 08:16

2006/07/16 08:47

2006/07/16 08:03

2006/07/16 08:23

2006/07/16 08:57

2006/07/16 08:54

2006/07/16 08:57

2006/07/16 08:57

2006/07/17 08:42

2006/07/18 08:34

2006/07/18 08:19

Edited by randallc
Link to comment
Share on other sites

  • Moderators

Nice randallc, I see the autoit code tags are still smiling at us :D

Edit:

PS, the last one I did, was more efficient I think.

Edit2:

Actually thanks for posting that randallc, I didn't notice what format he wanted, I just assumed it was the same in the file:

Local $sFilePath = @ScriptDir & '\locals.txt'
Local $aMonths[12] = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
Local $sFileRead = FileRead($sFilePath), $AllDates = ''

$aArray = StringRegExp(StringStripWS($sFileRead, 7), "(?: )(\D{0}\d{2}\D{0}\d{2}\D{0}\d{2}\D{0}\d{2})", 3)
If IsArray($aArray) Then
    For $xCount = 0 To UBound($aArray) - 1
        For $iCount = 0 To 11
            If StringInStr($aArray[$xCount], $aMonths[$iCount]) Then ExitLoop
        Next
        $aArray[$xCount] = StringReplace(StringStripWS(StringReplace($aArray[$xCount], _
                        $aMonths[$iCount], StringFormat('%02d', $iCount)), 8), ",'", '')
        $AllDates &= StringMid($aArray[$xCount], 5, 2) & '/' & _
        StringMid($aArray[$xCount], 1, 2) & '/' & _
        StringMid($aArray[$xCount], 3, 2) & ' ' & _
        StringMid($aArray[$xCount], 7, 5) & @CRLF
    Next
EndIf
$AllDates = StringTrimRight($AllDates, 2)
;MsgBox(0, '1', $AllDates)
FileWrite(@ScriptDir & '\localsDates.txt', $AllDates)
Run('notepad.exe "' & @ScriptDir & '\localsDates.txt"')
As you have a fix for my first one, I fixed my 2nd one.

Edit3:

Re-did it a bit for better speed.

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

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