AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

regexreplace help

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
elchapin



Joined: 06 Mar 2007
Posts: 64
Location: Columbus, OH, USA

PostPosted: Tue Jun 03, 2008 10:04 pm    Post subject: regexreplace help Reply with quote

I am trying to replace two different parts of a filepath:

The filepath is the 13th column of a csv file, and looks something like this:
V:\AEP\PEDD_Exports\CAMPBELL_20080512_001\OF_0001\AEPGC0000001\AEPGC0000001.mht

The first regexreplace works... I needed to get rid of everything before "\CAMPBELL". My second regexreplace doesn't work... I need to remove "AEPGC0000001" (each line in the csv increases in number 00000001, 0000002, 0000003, etc.). I think I have a variable problem.

Here's the variables...
Code:

        DocLinkField = 13
        DocLinkWhatToFind = PEDD_Exports
          DocLinkReplaceWithThis =
          DocLinkWhatToFindB = AEP(?=.*`\)
          DocLinkReplaceWithThisB = \


And the regexreplace is within a csv parsing loop. Here's the script:
Code:

        If A_Index = %DocLinkField%
           {
                 ; replace front (and back, if WhatToFind ends with .*)
                 FixedField := RegExReplace(A_LoopField, "s).*"DocLinkWhatToFind, DocLinkReplaceWithThis, ReplaceCount)
                 FixedField := RegExReplace(FixedField, "DocLinkWhatToFind", DocLinkReplaceWithThis, ReplaceCount)
                 FileAppend, `"%FixedField%`"`,, output.csv
           }


Here's the entire code, just in case:
Code:

; Creates text file name=field1 content=field2
; Doesn't create text file when the value in field2 is empty

Loop, read, C:\Users\ChipKohrman\Documents\1-PROJECTS\AHK_SCRIPTS\search_and_replace\ASCIIDATCAMPBELL_20080512_001 - Copy.csv
{

If A_LoopReadLine > 1
   FileAppend, `n, output.csv

    ; Loop, parse, current line being read, character that divides field, character to omit
    Loop, parse, A_LoopReadLine, ÿ, þ
    {
       
       
        CustodianField = 5
        CustodianWhatToFind = Buonaiuto.*
          CustodianReplaceWithThis = Joseph B Buonaiuto
          
        RecipField = 9
                  
        CCField = 10
        CCWhatToFind = ".`;.".*
          CCReplaceWithThis =
          
        BCCField = 11
        BCCWhatToFind = ".`;.".*
          BCCReplaceWithThis =
          
        DocLinkField = 13
        DocLinkWhatToFind = PEDD_Exports
          DocLinkReplaceWithThis =
          DocLinkWhatToFindB = AEP(?=.*`\)
          DocLinkReplaceWithThisB = \
        ;MsgBox, %DocLinkWhatToFindB%
       
       
       
        If A_Index = 1
           {
              CurrentField = `"%A_LoopField%`",
              FileAppend, %CurrentField%, output.csv
           }
        else
        If A_Index = %CustodianField%
           {
                 ; replace front (and back, if WhatToFind ends with .*)
                 FixedField := RegExReplace(A_LoopField, "s).*"CustodianWhatToFind, CustodianReplaceWithThis, ReplaceCount)
                 FileAppend, `"%FixedField%`"`,, output.csv
           }
        else
        If A_Index = %RecipField%
           {
                 StringReplace, FixedField, A_LoopField, `.`;, , all
                 StringReplace, FixedField, FixedField, `;`., , all
                 If FixedField = `.
                    {
                       FixedField =
                    }
                 FileAppend, `"%FixedField%`"`, , output.csv
           }
        else
        If A_Index = %CCField%
           {
                 StringReplace, FixedField, A_LoopField, `.`;, , all
                 StringReplace, FixedField, FixedField, `;`., , all
                 If FixedField = `.
                    {
                       FixedField =
                    }
                 FileAppend, `"%FixedField%`"`, , output.csv
           }
        else
        If A_Index = %BCCField%
           {
                 StringReplace, FixedField, A_LoopField, `.`;, , all
                 StringReplace, FixedField, FixedField, `;`., , all
                 If FixedField = `.
                    {
                       FixedField =
                    }
                 FileAppend, `"%FixedField%`"`, , output.csv
           }
        else                               
        If A_Index = %DocLinkField%
           {
                 ; replace front (and back, if WhatToFind ends with .*)
                 FixedField := RegExReplace(A_LoopField, "s).*"DocLinkWhatToFind, DocLinkReplaceWithThis, ReplaceCount)
                 FixedField := RegExReplace(FixedField, "DocLinkWhatToFind", DocLinkReplaceWithThis, ReplaceCount)
                 FileAppend, `"%FixedField%`"`,, output.csv
           }
        else
        {
        If A_Index != 1
           CurrentField = `"%A_LoopField%`",
           FileAppend, %CurrentField%, output.csv
        }
       
       
    }
}
return

Any help on this one, please? Sad
_________________
My startup is Telesaur - a telecommuting job site.
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Wed Jun 04, 2008 5:32 am    Post subject: Reply with quote

Code:
        If A_Index = %DocLinkField%
           {
                 ; replace front (and back, if WhatToFind ends with .*)
                 FixedField := RegExReplace(A_LoopField, "s).*"DocLinkWhatToFind)
                 FixedField := RegExReplace(FixedField, "AEPGC(\d{7})\\")
                 FileAppend, `"%FixedField%`"`,, output.csv
           }
DocLinkReplaceWithThis and ReplaceCount not needed
Back to top
elchapin



Joined: 06 Mar 2007
Posts: 64
Location: Columbus, OH, USA

PostPosted: Wed Jun 04, 2008 1:24 pm    Post subject: Reply with quote

That worked, thanks!

If I understand right, the (\d{7}) matches the first 7 digits?
And the first backslash makes the second backslash literal?
Code:

FixedField := RegExReplace(FixedField, "AEPGC(\d{7})\\")


If I wanted to replace a combination of letters, numbers, characters like OF_0001 would that look the same?
Code:

FixedField := RegExReplace(FixedField, "OF(\d{5})\\")

_________________
My startup is Telesaur - a telecommuting job site.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group