<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>润物无声 &#187; 正则表达式</title>
	<atom:link href="http://blog.zhourunsheng.com/tag/%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.zhourunsheng.com</link>
	<description>天空一朵雨做的云</description>
	<lastBuildDate>Sat, 08 May 2021 05:17:21 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>一种更符合自然语意的PHP正则表达式</title>
		<link>http://blog.zhourunsheng.com/2013/08/%e4%b8%80%e7%a7%8d%e6%9b%b4%e7%ac%a6%e5%90%88%e8%87%aa%e7%84%b6%e8%af%ad%e6%84%8f%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/</link>
		<comments>http://blog.zhourunsheng.com/2013/08/%e4%b8%80%e7%a7%8d%e6%9b%b4%e7%ac%a6%e5%90%88%e8%87%aa%e7%84%b6%e8%af%ad%e6%84%8f%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/#comments</comments>
		<pubDate>Thu, 15 Aug 2013 14:07:37 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[Web设计]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=1782</guid>
		<description><![CDATA[<p>在编程的过程中，尤其是文本的处理，我们很多时候都需要正则表达式，比如，验证用户输入的Email是否格式正确，电 [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2013/08/%e4%b8%80%e7%a7%8d%e6%9b%b4%e7%ac%a6%e5%90%88%e8%87%aa%e7%84%b6%e8%af%ad%e6%84%8f%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/">一种更符合自然语意的PHP正则表达式</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>在编程的过程中，尤其是文本的处理，我们很多时候都需要正则表达式，比如，验证用户输入的Email是否格式正确，电话号码是否符合规范，或者其他的自定义规则。对新手来说，熟记正则表达式的规则比较困难，那么有没有一种更符合自然语义的正则表达式呢，本文会给你答案。</p>
<p><img alt="verbalexp" src="http://www.codediesel.com/wp-content/uploads/2013/08/verbalexp.png" width="583" height="262" /></p>
<p>Most newbie (and some seasoned) programmers have difficultly constructing Regular Expressions. Many a times one needs to create a Regexp quickly to test a a particular piece of code. However, not being comfortable withe Regexps can be a problem. <a title="https://github.com/VerbalExpressions/PHPVerbalExpressions" href="https://github.com/VerbalExpressions/PHPVerbalExpressions" target="_blank">VerbalExpressions</a> is a PHP library that enables you to construct regular expressions using natural language like constructs. Think of it like a DSL for building Regexps.<span id="more-1782"></span><br />
verbalexpressions 已经实现了多个语言版本的库，可以从<a href="http://verbalexpressions.github.io/" target="_blank">这里</a>得到。</p>
<p>Below is a sample PHP code that constructs a regular expression which tests whether a given string is a valid url.</p>
<p>如下示例用来测试给定的一个string是否是有效的url地址。</p>
<pre>&lt;?php

include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  -&gt;startOfLine()
        -&gt;then("http")
        -&gt;maybe("s")
        -&gt;then("://")
        -&gt;maybe("www.")
        -&gt;anythingBut(" ")
        -&gt;endOfLine();

if($regex-&gt;test("http://www.codediesel.com"))
    echo "valid url";
else
    echo "invalid url";

?&gt;</pre>
<p>The main part of the code the DSL like interface that helps you build a Regexp.</p>
<pre>$regex  -&gt;startOfLine()
        -&gt;then("http")
        -&gt;maybe("s")
        -&gt;then("://")
        -&gt;maybe("www.")
        -&gt;anythingBut(" ")
        -&gt;endOfLine();</pre>
<p>If you want to see what regular expression the code has built, we can use the <strong>getRegex</strong> function of the class.</p>
<pre>&lt;?php

include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  -&gt;startOfLine()
        -&gt;then("http")
        -&gt;maybe("s")
        -&gt;then("://")
        -&gt;maybe("www.")
        -&gt;anythingBut(" ")
        -&gt;endOfLine();

echo $regex-&gt;getRegex();</pre>
<p>This will print the Regexp given below.</p>
<p>打印输出正则表达式。</p>
<pre>/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m</pre>
<p>We can now use the above Regexp in our code to accomplish the same thing as above.</p>
<pre>$myRegexp = '/^(http)(s)?(\:\/\/)(www\.)?([^ ]*)$/m';

if (preg_match($myRegexp, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}</pre>
<p>We can also use the <strong>$regex</strong> object given in the above example directly in our code where the particular Regexp is required.</p>
<p>可以使用函数 <strong>getRegex</strong> 获取正则表达式规则，或者直接可以用 <strong>$regex</strong> 对象，库内部已经实现了转换。</p>
<p>内部的转换函数：</p>
<pre>/**
* Object to string
*
* PHP Magic method to return a string representation of the object.
*
* @access public
* @return string
*/
    public function __toString()
    {
        return $this-&gt;getRegex();
    }</pre>
<pre>include_once('VerbalExpressions.php');

$regex = new VerEx;

$regex  -&gt;startOfLine()
        -&gt;then("http")
        -&gt;maybe("s")
        -&gt;then("://")
        -&gt;maybe("www.")
        -&gt;anythingBut(" ")
        -&gt;endOfLine();

if (preg_match($regex, 'http://www.codediesel.com')) {
    echo 'valid url';
} else {
    echo 'invalud url';
}</pre>
<p>The PHP version of VerbalExpressions is a port of the original JavaScript version,<a title="jsverbalexpressions" href="https://github.com/VerbalExpressions/JSVerbalExpressions" target="_blank">JSVerbalExpressions</a>. Additional modifiers that will help you build regular expressions can be found <a title="verbalexpressions wiki" href="https://github.com/VerbalExpressions/JSVerbalExpressions/wiki" target="_blank">here</a>.</p>
<p>文章节选：<a href="http://www.codediesel.com/php/constructing-hard-regular-expressions-with-verbalexpressions/" target="_blank">constructing-hard-regular-expressions-with-verbalexpressions</a></p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2013/08/%e4%b8%80%e7%a7%8d%e6%9b%b4%e7%ac%a6%e5%90%88%e8%87%aa%e7%84%b6%e8%af%ad%e6%84%8f%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/">一种更符合自然语意的PHP正则表达式</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2013/08/%e4%b8%80%e7%a7%8d%e6%9b%b4%e7%ac%a6%e5%90%88%e8%87%aa%e7%84%b6%e8%af%ad%e6%84%8f%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>实用的PHP正则表达式函数库</title>
		<link>http://blog.zhourunsheng.com/2011/07/%e5%ae%9e%e7%94%a8%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0%e5%ba%93/</link>
		<comments>http://blog.zhourunsheng.com/2011/07/%e5%ae%9e%e7%94%a8%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0%e5%ba%93/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 02:36:40 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[Web设计]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=486</guid>
		<description><![CDATA[<p>在平常的web开发中，经常需要处理文本的匹配和替换，以及表单的验证，下面这些实用的经过验证的代码相信会给开发带 [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/07/%e5%ae%9e%e7%94%a8%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0%e5%ba%93/">实用的PHP正则表达式函数库</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>在平常的web开发中，经常需要处理文本的匹配和替换，以及表单的验证，下面这些实用的经过验证的代码相信会给开发带来很多便利！</p>
<h3>1) <a href="http://forums.pcworld.co.nz/showthread.php?t=60179">String Mtach</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/String-Mtach.png"><img class="alignnone size-full wp-image-487" title="String Mtach" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/String-Mtach.png" alt="" width="550" height="92" /></a><br />
<span id="more-486"></span></p>
<h3>2) <a href="http://www.webcheatsheet.com/php/regular_expressions.php">Password Match Validation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Password-Match-Validation.png"><img class="alignnone size-full wp-image-488" title="Password Match Validation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Password-Match-Validation.png" alt="" width="550" height="94" /></a></p>
<h3>3) <a href="http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm">Email Address Validation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Email-Address-Validation.png"><img class="alignnone size-full wp-image-489" title="Email Address Validation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Email-Address-Validation.png" alt="" width="484" height="218" /></a></p>
<h3>4) <a href="http://roshanbh.com.np/2008/05/date-format-validation-php.html">Date Format Validation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Date-Format-Validation.png"><img class="alignnone size-full wp-image-490" title="Date Format Validation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Date-Format-Validation.png" alt="" width="550" height="142" /></a></p>
<h3>5)<a href="http://stackoverflow.com/questions/206059/php-validation-regex-for-url"> Validate URL</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Validate-URL.png"><img class="alignnone size-full wp-image-491" title="Validate URL" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Validate-URL.png" alt="" width="550" height="198" /></a></p>
<h3>6)<a href="http://www.daniweb.com/web-development/php/threads/65872"> Validate  URL using Preg_match</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Validate-URL-using-Preg_match.png"><img class="alignnone size-full wp-image-492" title="Validate  URL using Preg_match" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Validate-URL-using-Preg_match.png" alt="" width="550" height="261" /></a></p>
<h3>7)<a href="http://www.php.net/manual/en/function.ereg.php"> Email Validation Using ereg</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Email-Validation-Using-ereg.png"><img class="alignnone size-full wp-image-493" title="Email Validation Using ereg" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/Email-Validation-Using-ereg.png" alt="" width="550" height="261" /></a></p>
<h3>8 )<a href="http://roshanbh.com.np/2008/04/ip-address-validation-php.html"> IP Address Matching Valdation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/IP-Address-Matching-Valdation.png"><img class="alignnone size-full wp-image-494" title="IP Address Matching Valdation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/IP-Address-Matching-Valdation.png" alt="" width="550" height="193" /></a></p>
<h3>9)<a href="http://www.php.net/manual/en/function.ereg.php"> UK Postcode Validation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/UK-Postcode-Validation.png"><img class="alignnone size-full wp-image-495" title="UK Postcode Validation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/UK-Postcode-Validation.png" alt="" width="550" height="224" /></a></p>
<h3>10)<a href="http://www.php.net/manual/en/function.ereg.php"> SSN,ISBN and Zipcode Validation</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/SSNISBN-and-Zipcode-Validation.png"><img class="alignnone size-full wp-image-496" title="SSN,ISBN and Zipcode Validation" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/07/SSNISBN-and-Zipcode-Validation.png" alt="" width="550" height="261" /></a></p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/07/%e5%ae%9e%e7%94%a8%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0%e5%ba%93/">实用的PHP正则表达式函数库</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2011/07/%e5%ae%9e%e7%94%a8%e7%9a%84php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0%e5%ba%93/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
