-3

私は正規表現に本当に慣れていません。次の文字列でAmazonの料金を見つけるための正規表現を教えてください。フロートのリストが表示されます。

 private static List<float> GetAmazonFees(string text)
 {
      //Regular expression to fine list of fees
 }

Order ID: 102-8960168-7248227

Please ship this order using Standard shipping.

Ship by: 12/19/2012
Item: Ridata 8GB Class 10 SDHC Card - Secure Digital High Capacity -
Lightening Series - RDSDHC8G-LIG10
Condition: New
Condition note: This item is sealed and brand new! SHIPS IN ONE BUSINESS DAY
Listing ID: 1213M39KPDB
SKU: 7S-5JMI-HO98
Quantity: 1
Order date: 12/17/2012
Price: $8.60
Amazon fees: -$1.14
Shipping: $4.49
Your earnings: $11.95Ship by: 12/19/2012Item: RiDATA Lightning Series 16GB Secure Digital High-Capacity (SDHC)Condition: NewCondition note: This item is sealed and brand new! SHIPS IN ONE BUSINESS DAYListing ID: 1204MRSEY95SKU: HJ-EYF8-EZVRQuantity: 1Order date: 12/17/2012Price: $15.97Amazon fees: -$1.78Shipping: $4.99Your earnings: $19.18- - - - - - - - - - - - - - - - - - -NEXT STEPS FOR THIS ORDER:1) Print the packing slip:Log into your seller account and go to the Order Details page for thisorder: https://www.amazon.com/manageorderdetails?orderID=102-8960168-7248227.Click "Print order packing slip" next to the order number at the top of thepage.2) Buy shipping (optional):You may ship the item using any carrier and method you prefer. Want toavoid a trip to the post office? Click "Buy shipping" at the bottom of theOrder Detail page to purchase and print shipping labels from your home oroffice. Delivery confirmation is also available. To learn more, search"shipping" in seller Help.3) Confirm shipment:Click "Confirm shipment" at the bottom of the Order Detail page and entershipping details. Once confirmed, we'll charge the buyer, notify them theirorder has shipped, and transfer the order payment into your seller account.To lea 
4

1 に答える 1

4

これはかなり簡単です。必要な正規表現はのようになりますAmazon fees: (\-?)\$(\d+\.?\d*)\D

したがって、これを使用して次のことができます。

var matches = Regex.Matches(INPUT_STRING,@"Amazon fees: (\-?)\$(\d+\.?\d*)\D",RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);

これにより、すべての一致のリストが取得されます。次に、フロート用に処理します。

var fees = new List<float>();

foreach(Match match in matches) {
   float fee = 0;

   if (match.Groups[1].Value == "") {
       fee = float.Parse(match.Groups[2].Value);
   } else {
       fee = float.Parse('-' + match.Groups[2].Value);
   }
   fees.Add(fee);
}

私はネガティブをキャプチャしますが、すべての料金がネガティブであると想定できますが、それをチェックして正しいネガティブフロートを返したいと思います。

これはあなたが望むものを与えるはずだと思います。私はここで多くのエラーチェックを見落としていますが、あなたはその考えを理解しています。

于 2013-01-30T10:35:35.243 に答える