Using stringreplace to only replace a variable in a specific position

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Laie
Posts: 55
Joined: 18 Mar 2016, 06:45

Using stringreplace to only replace a variable in a specific position

10 Apr 2016, 10:36

I have been using stringreplace to change certain words in an XML document (after converting it to a .txt file - it will then be converted back) like this:

Code: Select all

FileRead, fileText1, %filepath1%
StringReplace, NewFileText1, fileText1, SEARCHTERM1, %ReplacementPhrase%, All
I have always wanted to replace all of the instances of the search term items in the document up to now. That hasn't been a problem.

Now I want to only replace a phrase, for instance "non-us-residency" with "us-residency" in relation to certain other search terms, for instance "SEARCHLast3" here.

The desired code would replace "non-us-residency" below with "us-residency" but ONLY after certain terms (e.g. that are five lines above the term to be replaced here), for instance SEARCHLast1 and SEARCHLast3, but ONLY those phrases, not SEARCHLast 2, for instance.

Code: Select all

>SEARCHLast3</lastName
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio
>non-us-residency</ResidencyRadio
I'd greatly appreciate any suggestions as to how to do this. I can replace all of the terms, and the first instance of the term, but not specific terms like this.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Using stringreplace to only replace a variable in a specific position

10 Apr 2016, 11:11

I can't say for certain but you may be better of using a COM object to go through and replace the text in certain tags. https://autohotkey.com/board/topic/5698 ... ntry367838
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Using stringreplace to only replace a variable in a specific position

10 Apr 2016, 13:47

tried with loop

Code: Select all

; replace non-us-residency  with us-residency when SEARCHLast1  or SEARCHLast3

#NoEnv
#Warn
SetBatchLines -1

SEARCHTERM1:="non-us-residency"
replacewith:="us-residency"
f1=%a_scriptdir%\NEWtext.txt
e4x:=""

e4x=
(Join`r`n
>SEARCHLast1</lastName
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio
>non-us-residency</ResidencyRadio1
>--------------------<
>SEARCHLast2</lastName
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio
>non-us-residency</ResidencyRadio2
>--------------------<
>SEARCHLast3</lastName
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio
>non-us-residency</ResidencyRadio3
>--------------------<
)

aa=0
e:=""
y:=""
x:=""
Loop,parse,e4x,`n,`r
  {
  x:= A_LoopField
  if x contains >SEARCHLast3<
    aa=1
  if x contains >SEARCHLast1<
    aa=1
  if aa=1
    {
    if x contains %searchterm1%
      {
      aa=0
      stringreplace,xn,x,%searchterm1%,%replacewith%,all
      e .= xn . "`r`n"
      continue
      }
    }
  e .= x . "`r`n"
  }
;msgbox,%e%
if e<>
 {
 ifexist,%f1%
     filedelete,%f1%
 fileappend,%e%,%f1%
 e:=""
 run,%f1%
 }
return

User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Using stringreplace to only replace a variable in a specific position

11 Apr 2016, 08:03

Please provide a full example of an XML document you wish to parse. Change any sensitive information but leave the XML's structure intact.
Laie
Posts: 55
Joined: 18 Mar 2016, 06:45

Re: Using stringreplace to only replace a variable in a specific position

13 Apr 2016, 02:37

Hello sinkfaze, the document to be parsed is here (after it is exported to an XML or XDP file):

http://www.uspto.gov/sites/default/file ... ia0014.pdf

When I export the PDF form as an XML file, I get this massive structure:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="XFA2_4" APIVersion="3.6.14118.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2015-11-02T13:24:24Z" uuid="75bf47c6-765a-4011-9846-0e86125a2994">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
><xfa:data
><us-request
><ContentArea1
><chkSecret
>0</chkSecret
><sfApplicantInformation
><sfAuth
><appSeq
>1</appSeq
></sfAuth
><sfApplicantName
><prefix
/><suffix
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio
>us-residency</ResidencyRadio
></resCheck
><sfUSres
><rsCityTxt
/><rsStTxt
/><rsCtryTxt
/></sfUSres
><sfNonUSRes
><nonresCity
/><nonresCtryList
/></sfNonUSRes
><sfMil
><actMilDropDown
/></sfMil
></sfAppResChk
><sfCitz
><CitizedDropDown
/></sfCitz
><sfApplicantMail
><mailCountry
/><postcode
/><address1
/><address2
/><city
/><state
/></sfApplicantMail
><sfInventorRepInfo
><sfReporgChoice
><chkOrg
/><org
><orgName
/></org
><sfRepApplicantName
><prefix
/><firstName
/><middleName
/><lastName
/><suffix
/></sfRepApplicantName
></sfReporgChoice
><sfRepAppResChk
><resCheck
/><sfUSres
><rsCityTxt
/><rsStTxt
/><rsCtryTxt
/></sfUSres
><sfNonUSRes
><nonresCity
/><nonresCtryList
/></sfNonUSRes
><sfMil
><actMilDropDown
/></sfMil
></sfRepAppResChk
><sfRepCitz
><CitizedDropDown
/></sfRepCitz
><sfRepApplicantMail
><address1
/><address2
/><city
/><state
/><postcode
/><mailCountry
/></sfRepApplicantMail
></sfInventorRepInfo
></sfApplicantInformation
></ContentArea1
><ContentArea2
><sfCorrepondInfo
><corresInfoChk
>0</corresInfoChk
></sfCorrepondInfo
><sfCorrCustNo
><customerNumber
/></sfCorrCustNo
><sfCorrAddress
><Name1
/><Name2
/><address1
/><address2
/><city
/><state
/><corrCountry
/><postcode
/><phone
/><fax
/></sfCorrAddress
><sfemail
><email
/></sfemail
><sfInvTitle
/><sfversion
/><sfAppinfoFlow
><sfAppPos
><chkSmallEntity
>0</chkSmallEntity
><class
/><subclass
/><us_suggested-tech_center
/><us-total_number_of_drawing-sheets
/><us-suggested_representative_figure
/><application_type
/><us_submission_type
/></sfAppPos
></sfAppinfoFlow
><sfPlant
><latin_name
/><variety
/></sfPlant
><sffilingby
><app
/><date
/><intellectual
/></sffilingby
><sfPub
><early
>0</early
><nonPublication
/></sfPub
><sfRepHeader
/><sfAttorny
><sfrepheader
><attornyChoice
>customer-number</attornyChoice
></sfrepheader
><sfAttornyFlow
><sfcustomerNumber
><customerNumberTxt
/></sfcustomerNumber
><sfAttrynyName
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/><attrnyRegNameTxt
/><attsequence
>1</attsequence
></sfAttrynyName
><sfrepcfr119
><repcfr119RegNameTxt
/><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/><repsequence
>1</repsequence
></sfrepcfr119
></sfAttornyFlow
></sfAttorny
><sfDomContinuityHeader
/><sfDomesticContinuity
><sfDomesContinuity
><sfdomesContAppStat
><domAppStatusList
/><domsequence
>1</domsequence
></sfdomesContAppStat
></sfDomesContinuity
><sfDomesContInfo
><domappNumber
/><domesContList
/><domPriorAppNum
/><DateTimeField1
/></sfDomesContInfo
><sfDomesContinfoPatent
><patAppNum
/><domesContList
/><patContType
/><patprDate
/><patPatNum
/><patIsDate
/></sfDomesContinfoPatent
></sfDomesticContinuity
><sfForeignPriorityHeader
/><sfForeignPriorityInfo
><frprAppNum
/><accessCode
/><frprctryList
/><frprParentDate
/><prClaim
/><forsequence
>1</forsequence
></sfForeignPriorityInfo
><sfpermit
><check
/></sfpermit
><AIATransition
><AIACheck
>0</AIACheck
></AIATransition
><authorization
><IP
>0</IP
><EPO
>0</EPO
></authorization
><sfAssigneeHeader
/><sfAssigneeInformation
><sfAssigneebtn
><appSeq
>1</appSeq
><lstInvType
/><LegalRadio
/></sfAssigneebtn
><sfAssigneorgChoice
><chkOrg
>0</chkOrg
><sforgName
><orgName
/></sforgName
></sfAssigneorgChoice
><sfApplicantName
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/></sfApplicantName
><sfAssigneeAddress
><address-1
/><address-2
/><city
/><state
/><postcode
/><phone
/><fax
/><txtCorrCtry
/></sfAssigneeAddress
><sfAssigneeEmail
/></sfAssigneeInformation
><NonApplicantHeader
/><sfNonApplicantInfo
><sfNonAsigneeBtn
><appSeq
>1</appSeq
></sfNonAsigneeBtn
><sfNonapplicantOrg
><chkOrg
>0</chkOrg
><sfNonOrg
><orgName
/></sfNonOrg
></sfNonapplicantOrg
><sfApplicantName
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/></sfApplicantName
><sfAssigneeAddress
><address-1
/><address-2
/><city
/><state
/><postcode
/><phone
/><fax
/><txtCorrCtry
/><email
/></sfAssigneeAddress
></sfNonApplicantInfo
><sfSignature
><sfSigHeader
/><sfSig
><registration-number
/><last-name
/><signature
/><date
/><first-name
/></sfSig
></sfSignature
></ContentArea2
><ContentArea3
/><invention-title
/><attorney-docket-number
/><version-info
>2.0</version-info
><clientversion
>11.007</clientversion
><numofpages
>8</numofpages
></us-request
></xfa:data
><dd:dataDescription xmlns:dd="http://ns.adobe.com/data-description/" dd:name="us-request"
><us-request
><ContentArea1
><chkSecret
/><sfApplicantInformation dd:maxOccur="-1"
><sfAuth
><appSeq
/></sfAuth
><sfApplicantName
><prefix
/><firstName dd:minOccur="0" dd:nullType="exclude"
/><middleName dd:minOccur="0" dd:nullType="exclude"
/><lastName dd:minOccur="0" dd:nullType="exclude"
/><suffix
/></sfApplicantName
><sfAppResChk
><resCheck
><ResidencyRadio dd:minOccur="0" dd:nullType="exclude"
/><RadioButtonList dd:minOccur="0" dd:nullType="exclude"
/></resCheck
><sfUSres
><rsCityTxt
/><rsStTxt
/><rsCtryTxt
/></sfUSres
><sfNonUSRes
><nonresCity
/><nonresCtryList
/></sfNonUSRes
><sfMil
><actMilDropDown
/></sfMil
></sfAppResChk
><sfCitz
><CitizedDropDown
/></sfCitz
><sfApplicantMail
><mailCountry
/><postcode
/><address1
/><address2
/><city
/><state
/></sfApplicantMail
><sfInventorRepInfo
><sfReporgChoice
><chkOrg
/><org
><orgName
/></org
><sfRepApplicantName
><prefix
/><firstName
/><middleName
/><lastName
/><suffix
/></sfRepApplicantName
></sfReporgChoice
><sfRepAppResChk
><resCheck
><ResidencyRadio dd:minOccur="0" dd:nullType="exclude"
/><RadioButtonList dd:minOccur="0" dd:nullType="exclude"
/></resCheck
><sfUSres
><rsCityTxt
/><rsStTxt
/><rsCtryTxt
/></sfUSres
><sfNonUSRes
><nonresCity
/><nonresCtryList
/></sfNonUSRes
><sfMil
><actMilDropDown
/></sfMil
></sfRepAppResChk
><sfRepCitz
><CitizedDropDown
/></sfRepCitz
><sfRepApplicantMail
><address1
/><address2
/><city
/><state
/><postcode
/><mailCountry
/></sfRepApplicantMail
></sfInventorRepInfo
></sfApplicantInformation
></ContentArea1
><ContentArea2
><dd:group dd:maxOccur="-1"
><sfCorrepondInfo
><corresInfoChk
/></sfCorrepondInfo
><sfCorrCustNo
><customerNumber
/></sfCorrCustNo
><sfCorrAddress
><Name1
/><Name2
/><address1
/><address2
/><city
/><state
/><corrCountry
/><postcode
/><phone
/><fax
/></sfCorrAddress
><sfemail dd:maxOccur="3"
><email
/></sfemail
><sfInvTitle
/><sfversion
/><sfAppinfoFlow
><sfAppPos
><chkSmallEntity
/><class
/><subclass
/><us_suggested-tech_center
/><us-total_number_of_drawing-sheets
/><us-suggested_representative_figure
/><application_type
/><us_submission_type
/></sfAppPos
></sfAppinfoFlow
><sfPlant
><latin_name
/><variety
/></sfPlant
><sffilingby
><app
/><date
/><intellectual
/></sffilingby
><sfPub
><early
/><nonPublication
/></sfPub
><sfRepHeader dd:dataNode="dataGroup"
/><sfAttorny
><sfrepheader
><attornyChoice
/></sfrepheader
><sfAttornyFlow
><sfcustomerNumber
><customerNumberTxt
/></sfcustomerNumber
><sfAttrynyName dd:maxOccur="10"
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/><attrnyRegNameTxt
/><attsequence
/></sfAttrynyName
><sfrepcfr119 dd:maxOccur="10"
><repcfr119RegNameTxt
/><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/><repsequence
/></sfrepcfr119
></sfAttornyFlow
></sfAttorny
><sfDomContinuityHeader dd:dataNode="dataGroup"
/><sfDomesticContinuity dd:maxOccur="-1"
><sfDomesContinuity
><sfdomesContAppStat
><domAppStatusList
/><domsequence
/></sfdomesContAppStat
></sfDomesContinuity
><sfDomesContInfo
><domappNumber
/><domesContList
/><domPriorAppNum
/><DateTimeField1
/></sfDomesContInfo
><sfDomesContinfoPatent
><patAppNum
/><domesContList
/><patContType
/><patprDate
/><patPatNum
/><patIsDate
/></sfDomesContinfoPatent
></sfDomesticContinuity
><sfForeignPriorityHeader dd:dataNode="dataGroup"
/><sfForeignPriorityInfo dd:maxOccur="-1"
><frprAppNum
/><accessCode
/><frprctryList
/><frprParentDate
/><prClaim
/><forsequence
/></sfForeignPriorityInfo
><sfpermit dd:maxOccur="-1"
><check
/></sfpermit
><AIATransition
><AIACheck
/></AIATransition
><authorization
><IP
/><EPO
/></authorization
><sfAssigneeHeader dd:dataNode="dataGroup"
/><sfAssigneeInformation dd:maxOccur="-1"
><sfAssigneebtn
><appSeq
/><lstInvType
/><LegalRadio
/></sfAssigneebtn
><sfAssigneorgChoice
><chkOrg
/><sforgName
><orgName
/></sforgName
></sfAssigneorgChoice
><sfApplicantName
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/></sfApplicantName
><sfAssigneeAddress
><address-1
/><address-2
/><city
/><state
/><postcode
/><phone
/><fax
/><txtCorrCtry
/></sfAssigneeAddress
><sfAssigneeEmail
><email dd:maxOccur="3" dd:minOccur="0" dd:nullType="exclude"
/></sfAssigneeEmail
></sfAssigneeInformation
><NonApplicantHeader dd:dataNode="dataGroup"
/><sfNonApplicantInfo dd:maxOccur="-1"
><sfNonAsigneeBtn
><appSeq
/></sfNonAsigneeBtn
><sfNonapplicantOrg
><chkOrg
/><sfNonOrg
><orgName
/></sfNonOrg
></sfNonapplicantOrg
><sfApplicantName
><prefix
/><first-name
/><middle-name
/><last-name
/><suffix
/></sfApplicantName
><sfAssigneeAddress
><address-1
/><address-2
/><city
/><state
/><postcode
/><phone
/><fax
/><txtCorrCtry
/><email
/></sfAssigneeAddress
></sfNonApplicantInfo
><sfSignature dd:maxOccur="-1"
><sfSigHeader dd:dataNode="dataGroup"
/><sfSig
><registration-number
/><last-name
/><signature
/><date
/><first-name
/></sfSig
></sfSignature
></dd:group
></ContentArea2
><ContentArea3 dd:dataNode="dataGroup"
/><invention-title
/><attorney-docket-number
/><version-info
/><clientversion
/><numofpages
/></us-request
></dd:dataDescription
></xfa:datasets
>
<pdf href="aia0014.pdf" xmlns="http://ns.adobe.com/xdp/pdf/"
/></xdp:xdp>
StringReplaceNth

Re: Using stringreplace to only replace a variable in a specific position

13 Apr 2016, 06:40

Just for reference, functions by sinkfaze & vxe which adds the ability to replace nth instance of a matching text string (one regex, one non-regex)
http://autohotkey.com/board/topic/66994 ... /?p=423972
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Using stringreplace to only replace a variable in a specific position

13 Apr 2016, 08:34

this is my sloppy attempt at it. It may change pending on how the xml looks with multiple applicants in it. Seems to work as-is for me.

Code: Select all

names := "test1|test2" ;pipe delimited list of names that you are looking for
file := "xml.xml" ;whatever your xml filename is

oxml := ComObjCreate("MSXML2.DOMDocument.6.0")
oxml.async := False
doc := oxml.load(file)
oxml.SetProperty("SelectionNamespaces", "xmlns:xdp='" oxml.namespaces[0] "' xmlns:xfa='" oxml.namespaces[1] "'")

rr := oxml.selectSingleNode("/xdp:xdp/xfa:datasets/xfa:data/us-request/ContentArea1").getElementsByTagName("lastName")
loop, % rr.length
{
	index := A_Index - 1
	loop, parse, names, |
		if (A_LoopField = name := rr.item(index).text)
			rr.item(index).selectSingleNode("../../sfAppResChk/resCheck/ResidencyRadio").text := "us-residency"
}

oxml.save(file)
oxml := ""

ExitApp
Laie
Posts: 55
Joined: 18 Mar 2016, 06:45

Re: Using stringreplace to only replace a variable in a specific position

13 Apr 2016, 09:29

kczx3: Thank you for the effort. I am clueless about this stuff right now, but I am going to play around with your script and try to understand it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, doanmvu, Google [Bot], mamo691 and 366 guests