正则表达式

preg_match 中常用的 isU 的解释
i: 表示in-casesensitive,即大小写不敏感
s: PCRE_DOTALL,表示点号可以匹配换行符。
U: 表示PCRE_UNGREEDY,表示非贪婪,相当于perl/python语言的.*?,在匹配过程中,对于.*正则,一有匹配立即执行,而不是等.*消费了所有字符再一一回退。





举例:

if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}

echo "<p></p>";

if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}


echo "<p></p>";

preg_match_all("/http:\/\/([^\/|\.]+)/is","http://www.php.net/index.html,http://hh.163.com/test/hi.php", $matches);
print_r($matches);

echo "<p></p>";

preg_match_all("/http:\/\/([^\/]+)/is","http://www.php.net/index.html,http://www.163.com/test/hi.php", $matches);
print_r($matches);

echo "<p></p>";

$content = "<img src=\"http://www.wangyou.com/icon.gif\" width=\"522\" height=\"600\"/> sdfdsfdsf <IMG src=\"http://www.163.com/163.gif\" width=\"300\" height=\"400\"/>";

// 分析文章内容得到图片并得到微缩图路径 pic icon,取首张图片的路径
preg_match_all("/(<img|<IMG){1} src=[\"]([^\"]+)/",$content,$array);
print_r($array);

echo "<p></p>";

// 分析文章内容得到图片并得到微缩图路径 pic icon,取首张图片的路径
preg_match_all("/<img src=[\"](.*)[\"]+/iU",$content,$array);
print_r($array);

echo preg_replace ("/web/i","i`m here", "PHP is the web scr  webipting language of choice.");

关于贪婪模式(和正则表达式结尾有关,有限定的要加U,反之则不用):

preg_match_all("/http:\/\/([^\/]+)/i","http://www.php.net/index.html,http://www.163.com/test/hi.php", $matches);
print_r($matches); // 这个就不要加U

echo "<p></p>";


$content = "<img src=\"http://www.wangyou.com/icon.gif\" width=\"522\" height=\"600\"/> sdfdsfdsf <IMG src=\"http://www.163.com/163.gif\" width=\"300\" height=\"400\"/>";

// 分析文章内容得到图片并得到微缩图路径 pic icon,取首张图片的路径
// 这个就要加U
preg_match_all("/<img src=[\"](.*)[\"]/iU",$content,$array); 
print_r($array);




        $string = "April 15, 2003";
        $pattern = "/([a-zA-Z]+) ([0-9]+), (\d+)/i";
        //$replacement = "\${1}1,\$3";
       
        $replacement = "the day is \$2,the month is \${1}1,the year is \$3";
       
        echo  preg_replace($pattern, $replacement, $string);
echo "<p></p>";echo "<p></p>";



$c = " , , ";

echo preg_replace("/\[img\](.+)\[\/img\]/iU","<img src=\"\${1}\"/>",$c);  // 这个就要加U

你可能感兴趣的:(PHP,.net,正则表达式,python,perl)