php知識點溫習之Regex

來源:互聯網
上載者:User
php知識點複習之Regex

//Regex的技術知識


// []定義字元集和 example [a-z] [A-Z] [0-9] [\f\r\n\t]


//定位器^[A-Z][0-9]$開頭結尾


//量詞 * + ? {n} {m,n} {n.}指的都是重複次數


//選擇 |


//可列印字元:ASCII中33-127表示的字元,就是我們看得見的符號 空格,刪除,斷行符號,取消等等就典型的是不可列印字元


//POSOXRegex
//常用函數
echo "
";
$b4 = "abc";
$pattern = "[[:alpha:]]";
echo ereg($pattern,$b4);
//輸出1
//eregi() 無符號的


$b7 = "asd&[email protected]&adfdf";

$pattern1 = "&";
$pattern2 = "[&@]";
print_r(split($pattern1,$b7));
print_r(split($pattern2,$b7));


echo "
";
$b8 = "helloworld";
$pattern3 = "world";
$replacement1 = "persion";
echo ereg_replace($pattern3,$replacement1,$b8);


echo "
";
echo sql_regcase("abcdefg");
//輸出:[Aa][Bb][Cc][Dd][Ee][Ff][Gg]


//Perl Regex perl是在支援posix的基礎上額外的擴充 \d數字 \D非數字 \s空白 \S非空白 \w字母數學符號底線 \W非字母數字記號底線

//常用函數array preg_grep ( string pattern, array input )
echo "
";
$w1 = array("adad","adad4","asda","1asf3","sdfs");
$fl_array = preg_grep ("/^\D{1,}$/", $w1);//此處強調一點{1,}而不是posix中的{1.}
print_r($fl_array);


echo "
";
echo preg_match ("/a/","abc");
//輸出1


echo "
";
//\b表示單詞的邊界
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.";
}


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 "
";
$pattern5 = "/[\*]+/";
$w3 = "aaa***bbb*ccc";
print_r(preg_split($pattern5,$w3));


echo "
";
$pattern6 = "/world/";
$content2 = "girl";
$tre = "hello world";
echo preg_replace($pattern6,$content2,$tre);


//string preg_quote ( string str [, string delimiter] )轉義
echo "
";
$keywords = "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");//此處的/若是不加,就不會轉義g3/400中的/;
echo $keywords;

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.