2012-02-20 11 views
1

RegexBuddyを購入しました。静的で単純なものにマッチしない限り、試してみてください。正規表現を取得してください!正規表現を抽出して配列に追加する

私がしようとしていることは、次のテキスト行からです。潮汐情報を連想配列に抽出したいと思います。

High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am

そして次の配列で終わる:

[0] = 
    'Day' => 'Mon', 
    'Time' => '8.54pm', 
    'Height' => '2.0m', 
    'Tide' => 'High' 

[1] = 
    'Day' => 'Tue', 
    'Time' => '09.18am', 
    'Height' => '2.4m', 
    'Tide' => 'High' 

私が最も苦労していますコンセプトは、私が(例えば2.0メートルを抽出したい複数の一致があるという事実であると2.4 m)。私は2.0mと2.4mでマッチすることができましたが、どれがどれを決定するのですか? (第1高潮対第2高潮)。

ヒント

+1

あなたが火の上に潮が "Low" であるという情報を手に入れたが印刷されますか? – Basti

+0

「Hide Tide:」は常に存在するため、直後の情報は隠れ潮とみなすことができます。 "and"の後のすべてが、その日の2番目の満潮です。言い換えれば、「Hide tide:」、「on」、「at」、「and」、「on」、「at」は常に存在し静的です。 –

+1

実際、私はふわふわしているかもしれません。そのテキスト行は、1日の2つの満潮のためのものです。私は編集します。 –

答えて

2
$string = "High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am"; 

preg_match_all("~((High|Low) Tide:)? (\d.\dm) on (\w{3}) at (.{7})~", $string, $matches, PREG_SET_ORDER); 

var_dump($matches); 

出力

array 
    0 => 
    array 
     0 => string 'High Tide: 2.0m on Mon at 08.54pm' (length=33) 
     1 => string 'High Tide:' (length=10) 
     2 => string 'High' (length=4) 
     3 => string '2.0m' (length=4) 
     4 => string 'Mon' (length=3) 
     5 => string '08.54pm' (length=7) 
    1 => 
    array 
     0 => string ' 2.4m on Tue at 09.18am' (length=23) 
     1 => string '' (length=0) 
     2 => string '' (length=0) 
     3 => string '2.4m' (length=4) 
     4 => string 'Tue' (length=3) 
     5 => string '09.18am' (length=7) 
私はおそらくので、ここで間違っ干潮についての事を得た3210

いくつかのコードが潮なしで

$string = "High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am"; 

preg_match_all("~(\d.\dm) on (\w{3}) at (.{7})~", $string, $matches, PREG_SET_ORDER); 

var_dump($matches); 

出力:

array 
    0 => 
    array 
     0 => string '2.0m on Mon at 08.54pm' (length=22) 
     1 => string '2.0m' (length=4) 
     2 => string 'Mon' (length=3) 
     3 => string '08.54pm' (length=7) 
    1 => 
    array 
     0 => string '2.4m on Tue at 09.18am' (length=22) 
     1 => string '2.4m' (length=4) 
     2 => string 'Tue' (length=3) 
     3 => string '09.18am' (length=7) 
+0

!ありがとう。あなたの答えは私が最後に受け入れたものですが、ちょっとした変更を加えただけで、私は理解していた最小限の正規表現で、後のことを正確に得ることができました。 –

1

単語andが常に2つの潮を区切っている場合は、文字列を2つに分割し、各半分を別々に処理できます。

$str = "High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am"; 
$data = explode(" and ", $str); 

$result = array(); 
foreach($data as $tide) 
{ 
    $result[] = parseWithRegex($tide); 
} 
1

あなたがnamed groupsを使用して、あなたが名前によってキャプチャ何を参照することができます:たとえば(?P<name>exp) =>$yourVarName['name']

(テストが、これはアイデアだろうではない)

/^[^\d]+(?P<heightOne>[\d\.]+?m)\son\s(?P<dayOne>\w+?)\sat\s(?P<timeOne>.*?(am|pm))\sand\s(?P<heightTwo>[\d\.]+?m)\son\s(?P<dayTwo>\w+?)\sat\s(?P<timeTwo>.*?(am|pm))$/ 
+0

あなたは名前付きグループフォーマットを正しく表示しますが、あなたの正規表現では '?P:' – webbiedave

+0

@webbiedave:ありがとう、修正済みです。 – sweaver2112

+0

@ sweaver2112ありがとう!私はあなたのコードをコピーして実行したいだけでなく、それを見て、それがどのように動作しているのかを理解し、それに応じて(そして受け入れて)報告します。 –

0

あなたは結果を連想配列で取得するためにnamed capturing groupsを使用することができますし、文字列に一致するパターンはかなり単純です。

/(?P<tide>high|low)\s+tide:\s+(?P<height1>\d+\.\d+m)\s+on\s+(?P<day1>[a-z]+)\s+at\s+(?P<time1>\d+\.\d+[ap]m)\s+and\s+(?P<height2>\d+\.\d+m)\s+on\s+(?P<day2>[a-z]+)\s+at\s+(?P<time2>\d+\.\d+[ap]m)/i

スクリプト例:

$string = "High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am"; 

// named groups will also assign matches associative to the matches array, e.g. (?P<tide>high|low) will set $matches["tide"] to 'low' or 'high' 
preg_match(
     '/ 
      (?P<tide>high|low)      # match and capture string "high" or "low" 
      \s+tide:\s+        # match string "tide" surrounded with one or more spaces on each side 
      (?P<height1>\d+\.\d+m)     # match and capture one or more digits followed by a dot and one or more digits followed by an m 
      \s+on\s+        # match string "on" surrounded with one or more spaces on each side 
      (?P<day1>[a-z]+)      # match one or more letters 
      \s+at\s+        # match string "at" surrounded with one or more spaces on each side 
      (?P<time1>\d+\.\d+[ap]m)    # match and capture one or more digits followed by a dot and one or more digits followed by an a or p, and string "m", so am or pm 
      \s+and\s+        # match string "and" surrounded with one or more spaces on each side 
      (?P<height2>\d+\.\d+m)     # match and capture one or more digits followed by a dot and one or more digits followed by an m 
      \s+on\s+        # match string "on" surrounded with one or more spaces on each side 
      (?P<day2>[a-z]+)      # match one or more letters 
      \s+at\s+        # match string "at" surrounded with one or more spaces on each side 
      (?P<time2>\d+\.\d+[ap]m)    # match and capture one or more digits followed by a dot and one or more digits followed by an a or p, and string "m", so am or pm 
     /ix', $string, $matches); 

print_r($matches); 

これは

Array 
(
    [0] => High Tide: 2.0m on Mon at 08.54pm and 2.4m on Tue at 09.18am 
    [tide] => High 
    [1] => High 
    [height1] => 2.0m 
    [2] => 2.0m 
    [day1] => Mon 
    [3] => Mon 
    [time1] => 08.54pm 
    [4] => 08.54pm 
    [height2] => 2.4m 
    [5] => 2.4m 
    [day2] => Tue 
    [6] => Tue 
    [time2] => 09.18am 
    [7] => 09.18am 
) 
関連する問題