2016-04-23 90 views
-2

我剛剛開始在PCRE中學習Regex,並試圖編寫一個正則表達式,用於將字符串中的「women」替換爲「women」。 基本上這個字符串是一個包含「men」的文本(不區分大小寫),我想用「women」替換它 您的幫助非常感謝。替換正則表達式中的關鍵字(PCRE)

+1

這是最基本的正則表達式..你有什麼試過? – rock321987

+0

對於這麼簡單的'str_replace()'建議 – 2016-04-23 02:51:09

回答

0

考慮這個表達式與解釋:

<?php 
$string = "This is a man's world and it wouldn't be nothing without a woman or a girl."; 
$regex = '~  # opening delimiter 
      \b # word boundary 
      man # man literally 
      \b # word boundary 
      ~x'; # closing delimiter 

echo preg_replace($regex, "woman", $string); 
# This is a woman's world and it wouldn't be nothing without a woman or a girl. 
?> 

正如已經mentionned的意見,爲您具體的例子簡單的字符串函數將可能更合適。
要真正學習正則表達式,StackOverflow可能不是最合適的地方,最好去http://www.regular-expressions.infohttp://www.regexone.com對於初學者。