2015-11-23 77 views
0

我試圖從1'699.200颳起價格像1699.20正則表達式颳去價格沒有任何標籤

我該怎麼做,通過正則表達式exp?

Reservation source: Booking.com reservation #591064783 

The following information was provided by Booking.com at the time the reservation was created: 

    *** Reservation information *** 
    Reservation made on: 2015-11-08 at 23:54:52 
    Currency: EUR 
    Total reservation amount: 1'699.200 
    Total reservation commission amount: 254.880 

    *** Customer information *** 
    Name: Daniele 
    Email: [email protected] 
    Address: 
    Zip/Post code: 
    City: . 
    Country: Switzerland 
    Company: 
    Phone: +41761 
    Remarks: Hello, we might be 9 friends, and we can bring our own mattress would it be a problem? If it is, then we will be 8. 

    *** Room guest information *** 
    Room arrival date: 2015-12-29 
    Room departure date: 2016-01-02 
    Check-in: 2015-12-29 
    Check-out: 2016-01-02 
    Guest name: Daniele 
    Number of guests: 8 
    Smoking preference: Non-smoking 
    Remarks: 

    *** Room/facilities information *** 
    Room facilities: Tea/Coffee maker, Shower, Hairdryer, Iron, Refrigerator, Desk, Ironing facilities, Sitting area, Fan, Toilet, Microwave, Washing machine, Bathroom, Heating, Kitchen, Flat-screen TV, Sofa, Hardwood/Parquet floors, Dining area, Electric kettle, Kitchenware, Wardrobe/Closet, Oven, Stovetop, Cleaning products, City view, Towels, Linens, Dining table, Private apartment in building, Clothes rack 
    Room information: No meal is included in this room rate. Children and Extra Bed Policy: All children are welcome. One child under 3 years is charged EUR 10 per night in a crib. Any additional children under 4 years are charged EUR 10 per night for extra beds. The maximum number of extra beds/cribs in a room is 1. Deposit Policy: 20 percent of the total amount may be charged anytime after booking. Cancellation Policy: Please note, if canceled or modified, 100 percent of the total price of the reservation will be charged. 
    Room extra information: This apartment has a washing machine, seating area and oven. 

    *** Room pricing information *** 
    Currency: EUR 
    Total price: 1'699.200 
    Commission: 254.880 
    Nightly prices: 
    2015-12-29: 424.000 (rate: 4103701 "Standard Rate") 
    2015-12-30: 424.000 (rate: 4103701 "Standard Rate") 
    2015-12-31: 424.000 (rate: 4103701 "Standard Rate") 
    2016-01-01: 427.200 (rate: 4103701 "Standard Rate") 

這是我試過的,https://regex101.com/r/aE6vK0/1但這不是它。

+0

您使用什麼語言? – hwnd

+0

PHP,但我想通過1個表達式來完成它,比如Total預留量:(\ s + [^ \ s] +)。所以任何工作regex101應該這樣做 –

+1

一旦你得到了「1'699.200」字符串,使用你的編程語言刪除引號,並簡單地轉換爲數字。 – StephaneM

回答

1

不是試圖使用單個正則表達式對數字/非數字進行排序,而是創建一個正則表達式來匹配您的上下文,然後在結果數組上使用回調函數。

preg_match_all("~:\h*(\d+(?:['.]\d+)+)~", $str, $matches); 

$results = 
    array_map(function($n) { 
    return number_format(strtr($n, array("'"=>'')), 2, '.', ''); 
    }, $matches[1]); 

print_r($results); 

eval.in

+0

哇,這很好..謝謝 –

1

我不是PHP大師,但已經給出的評論似乎是一個很好的方向。對於每個模板,你可以簡單地針對每行運行下面的正則表達式:

Total reservation amount:\s(.*) 

我會認爲這場比賽上面將存入名爲$num一個PHP變量。您可以將它投射到double,然後根據需要使用它,例如

$num = 1'699.200; 
$num = str_replace("'", "", $num); 
$double_val = doubleval($num) 

既然你刮模板是他們自己可能都通過一個(確定性)計算機程序產生的,你不應該有應對隨機格式。相反,應該有一組格式,你可以通過查看一些模板來預期。

+0

這似乎很好,但不知道(雙)做什麼?不知道這樣的PHP函數和incase這是一個函數,我認爲它應該看起來像雙($ num); –

+0

嘗試使用'doubleval()'函數。由於您使用的模板很可能是由計算機程序生成的,因此您不必擔心意外的格式。 –

+0

@IlanHasanov不,該函數是'floatval()'的別名,並將值轉換爲float(請參閱[PHP手冊](http://php.net/manual/en/function.floatval.php)) – Jan