簡單的PHP方式:
<?php
$pattern = "/an/i";
$text = "banANA";
preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE);
preg_match($pattern, $text, $matches, 0, $matches[0][1]);
echo $matches[0];
?>
會給你 「AN」。
UPDATE:不知道它是一個替換。試試這個:
<?php
$toRemove = 'test';
$string = 'This is a test string to test to removing the word test';
preg_match("/$toRemove/", $string, $matches, PREG_OFFSET_CAPTURE);
$newString = preg_replace("/$toRemove/", "", $string);
$newString = substr_replace($newString, $matches[0][0], $matches[0][1], 0);
echo $newString;
?>
找到的第一個匹配並記住它,然後刪除一切,然後把什麼是第一點回
粘貼你的正則表達式和你正在使用的日期的例子。 – 2010-05-17 14:59:29
是否只想匹配第一次出現或第二次出現的所有事件? – webbiedave 2010-05-17 15:00:06
正則表達式:'\\ [n @。*?\\]' @webbiedave - 我想匹配除第一個外的所有事件。 – scott 2010-05-17 15:04:08