すべての価格を検索し、乗算 (たとえば 1.25) し、置き換える必要がある xml ファイルがあります。
値札は次のようになります。
<price><![CDATA[15.9]]></price>
操作後の値札は次のようになります。
<price><![CDATA[19.875]]></price>
これは、正規表現を使用して Notepad++ または PowerGrep で実行できますか?
前もって感謝します。
私の知る限り、どちらのプログラムでも計算を実行することはできませんが、任意の言語で簡単なプログラムを作成して、正規表現を使用してファイルを取得し、数値を見つけることができます。その文字列を double としてキャストして計算し、文字列に戻します。今日の後半には、おそらく c# で何かを作成できますが、ほとんどの言語では比較的簡単に作成できるはずです。Windows環境にいない場合、またはWindows用のPowershellを使用している場合は、シェルスクリプトを作成してgrepを使用することもできるかもしれませんが、Powershellの経験はあまりありません.
編集:これを行う簡単な方法がありますhttp://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx これは、本質的に xmldocument オブジェクトを使用して実行したいことです。
Edit2: 元のポスターを手に入れることができませんでしたが、誰かがその情報を使用できるのではないかと思い、多くのことを学びました。興味のある方がいれば、ソースコードを github に追加できます。
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;
}
Notepad++ には、ドキュメントと Notepad++ 自体にアクセスできるクイック Python スクリプトを作成できるPythonscriptプラグインがあります。
インストールとセットアップについては、この回答で説明しています。
それ以来、API は少し進化しており、現在はEditor.rereplaceで正規表現を置き換えています。
# 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()