簡體   English   中英

PHP正則表達式盡管未更改也停止工作

[英]PHP Regular expression stopped working although unchanged

//START GET DATES
$regexp = '/[0-9]{2,4}[-\/ ]{1}([A-Za-z]{3}|[0-9]{2})[-\/ ]{1}[0-9]{2,4}/i';

preg_match_all($regexp, $output, $dates);

//Dec 05, 1995 + December 5, 1995
$regexp = '/\b[[A-Za-z]{3,9}\b[ 0-9\,]{2,5}[0-9]{4}/i';
preg_match_all($regexp, $output, $dates);

//09 Aug 2012
$regexp = '/[0-9]{2}[ ]{1}[A-Za-z]{3}[ ]{1}[0-9]{4}/i';
preg_match_all($regexp, $output, $dates);
print_r($dates);

以上是我的正則表達式,用於從一堆文本中提取不同格式的日期。

該表達式運行良好,據我所記得,絕對沒有任何更改。

誰能告訴我這些表達式是否有問題,如果不是,還有什么其他原因可能導致這種突然中斷?

干杯

沒有更多的信息,很難給出准確的答案,但是有幾點需要注意:

  • 這些是一些草率的正則表達式。
    • [A-Za-z] ,然后選擇不區分大小寫的選項。
    • [[A-Za-z]
    • {1} (重復)。
    • 不必要的逃生,等等。 如果它們中也有錯誤,我也不會感到驚訝。
  • 您正在按順序應用正則表達式。 我不知道PHP,但是看起來以前的比賽結果被下一個preg_match_all覆蓋。 也許您確實有結果,但是它們被下一個正則表達式覆蓋,而下一個正則表達式卻沒有任何匹配?

因此,讓我們嘗試為您找到一個更好的正則表達式。 這個怎么樣:

preg_match_all(
    '%\b                  # Start at a word boundary
    (?:                   # Match the following:
     (?:                  # either
      \d+\b               # a number,
      (?:\.|st|nd|rd|th)* # followed by a dot, st, nd, rd, or th (optional)
      |                   # or a month name
      (?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)\b
     )
     [\s.,/-]*            # followed by a date separator, comma or whitespace (opt.)
    ){3}                  # Do this three times
    (?<!\s)               # Don\'t match trailing whitespace
    %ix', 
    $output, $dates, PREG_PATTERN_ORDER);
$dates = $dates[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM