Notepad++ or PowerGrep regex to find, multiply and replace a price

1.5k Views Asked by At

I have an xml file that i need to find, multiply (e.g. by 1.25) and replace all prices.

Price tag looks like that: <price><![CDATA[15.9]]></price>

The price tag should look like that after the operation: <price><![CDATA[19.875]]></price>

Can this be done in Notepad++ or PowerGrep using a regular expression?

Thanks in advance.

2

There are 2 best solutions below

5
thekidxp On

As far as I know you can't use either program to preform the math but you can build a simple program in most any language of your choice to take a file use regex to find the number. Cast that string as a double do the math and put it back into the string. Later on today I could probably build something in c# but it should be relatively straight forward in most languages. You may even be able to build a shell script and use grep if you're not in a windows environment or use Powershell for windows but I have less experience with Powershell.

Edit: There is an easier way to do this http://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx this is essentially what you want to do using the xmldocument object.

Edit2: I did this even though I couldn't get a hold of the original poster I thought someone might be able to use the info and I learned a lot. I can add the source code to github if anyone is interested.

public static void ChangePricesWork(string filepath, double multiply)
{
  var document = new XmlDocument();
  document.Load(filepath);
  XmlNodeList nodeList = document.GetElementsByTagName("price");

  foreach (XmlNode node in nodeList)
  {
      if (!string.IsNullOrEmpty(node.InnerText))
      {
         node.InnerText = Convert.ToString(multiplyPrice(multiply, node.InnerText));
      }

   }

   string newFilePath = string.Format(@"{0}\{1}_updated.xml", Path.GetDirectoryName(filepath),   Path.GetFileNameWithoutExtension(filepath)); 
   document.Save(newFilePath);
}

   private static double multiplyPrice(double multiply, string oldPrice)
   {
     var newPrice = new double();
     if (Double.TryParse(oldPrice, out newPrice))
     {
       newPrice = newPrice * multiply;
     }
     return newPrice;
   }
3
Matt On

Notepad++ has a Pythonscript plugin that allows you to create quick Python scripts that have access to your document and Notepad++ itself.

The install and setup is described in this answer.

The API has moved on a little bit since then, you do a regular expression replace with Editor.rereplace now.

# Start a sequence of actions that is undone and redone as a unit. May be nested.
editor.beginUndoAction()

# multiply_price_cdata
from decimal import *
TWOPLACES = Decimal(10) ** -2 

def multiply_price_cdata( m ):
    price = Decimal( m.group(2) ) * Decimal( 1.25 )
    return  m.group(1) + str(price.quantize(TWOPLACES)) + m.group(3)

def cdata( m ):
    return "CDATA"

# npp++ search/replace
re_price = r'(<price><!\[CDATA\[)(\d+\.\d+|\d+)(\]\]></price>)'
editor.rereplace( re_price , multiply_price_cdata )

# end the undo sequence
editor.endUndoAction()