-3

我对正则表达式真的很陌生。你能告诉我在以下字符串中查找亚马逊费用的正则表达式是什么。所以它给了我浮动列表。

 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 回答