使用正規表示式匹配完整句子後匹配特定單字
P粉872101673
P粉872101673 2024-04-01 14:38:05
0
2
396

我是新手。我試圖在下面的任一行中找到全名,並且沒有 Obituary for

<h2>Obituary for John Doe</h2>
<h1>James Michael Lee</h1>

我的正規表示式是這樣的。

(<h1>(.+?)<\/h1>|<h2>Obituary\sfor\s(.+?)<\/h2>)

我得到的仍然是 John Doe 的 訃告。如何刪除 訃聞?

P粉872101673
P粉872101673

全部回覆(2)
P粉775788723

你能在不使用正規表示式的情況下做這樣的事情嗎?

/**
 * @description : Function extracts names from html header tags
 * @example : "

Obituary for John Doe

James Michael Lee

" -> ["John Doe", "James Michael Lee"] * @param $html string * @return []string : list of full names */ function extractFullNames($html) { $regex = '/(.*?)/'; preg_match_all($regex, $html, $matches); $names = $matches[1]; $names = array_map('trim', $names); $names = array_map('strip_tags', $names); $names = array_map('strtolower', $names); $names = array_map('ucwords', $names); $names = array_map('removeObituary', $names); return $names; } /** * @description : Function used to remove "Obituary For" if present * @example : "Obituary For John Doe" -> "John Doe" * @param $name string * @return string : name without "Obituary For" */ function removeObituary($name) { $name = str_replace("Obituary For ", "", $name); return $name; } // Test cases $html = '

Obituary for John Doe

James Michael Lee

'; $names = extractFullNames($html); $expected = ['John Doe', 'James Michael Lee']; echo "Expected: " . implode(', ', $expected) . "\n"; echo "Actual: " . implode(', ', $names);
P粉394812277

條條大路通羅馬,你或許可以這樣做:

|2>Obituary\sfor\s)\K[^>

請在 regex101 查看此示範。匹配項將位於 $out[0] 中。

\K 重設 開頭報告比賽。有關詳細信息,請參閱 SO 正規表示式常見問題解答

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!