TransWikia.com

Find and Replace XML tag in np++ to specific details

Super User Asked by paresh patil on January 26, 2021

I have a XML file with following data

<Shipment>
 <Shipper>
  <Name>Clothes</Name>
  <Address1>Apparel Street</Address1>
  <Country>GB</Country>
 </Shipper>
 <Shopper>
  <Name>ABC</Name>
  <Address1>Street 1</Address1>
  <Country>IE</Country>
 </Shopper>
</Shipment>
<Shipment>
<Shipper>
  <Name>Clothes</Name>
  <Address1>Apparel Street</Address1>
  <Country>GB</Country>
 </Shipper>
 <Shopper>
  <Name>XYZ</Name>
  <Address1>Street 9</Address1>
  <Country>US</Country>
 </Shopper>
</Shipment>

I want to change only the Shopper addresses to specific one, like below

<Shipment>
<Shipper>
  <Name>Clothes</Name>
  <Address1>Apparel Street</Address1>
  <Country>GB</Country>
 </Shipper>
 <Shopper>
  <Name>ABC</Name>
  <Address1>Wonderland</Address1>
  <Country>CA</Country>
 </Shopper>
</Shipment>
<Shipment>
<Shipper>
  <Name>Clothes</Name>
  <Address1>Apparel Street</Address1>
  <Country>GB</Country>
 </Shipper>
 <Shopper>
  <Name>XYZ</Name>
  <Address1>Wonderland</Address1>
  <Country>CA</Country>
 </Shopper>
</Shipment>

Let me know if there is any easy way to resolve this using Find and replace option in Notepad++

thanks

One Answer

  • Ctrl+H
  • Find what: (<Shopper>(?:(?!</Shopper>)[sS])+?<Address1>).+?(</Address1>[sS]+?<Country>).+?(</Country>)
  • Replace with: $1Wonderland$2CA$3
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(                               # group 1
  <Shopper>                       # open tag
  (?:(?!</Shopper>)[sS])+?      # 1 or more any character, not greedy, and never encountered "</Shopper>"
  <Address1>                      # open tag
)                               # end group 1
.+?                             # 1 or more any character but ewline, not greedy
(                               # group 2
  </Address1>                     # close tag
  [sS]+?                        # 1 or more any character, not greedy
  <Country>                       # open tag
)                               # end group 2
.+?                             # 1 or more any character but newline
(</Country>)                    # group 3, end tag

Replacement:

$1                  # content of group 1
Wonderland          # literally
$2                  # content of group 2
CA                  # literally
$3                  # content of group 3

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Correct answer by Toto on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP