<?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; languages</title>
	<atom:link href="http://iregex.org/blog/tag/languages/feed" rel="self" type="application/rss+xml" />
	<link>http://iregex.org</link>
	<description>原创、翻译、转载关于正则表达式的文章</description>
	<lastBuildDate>Sun, 27 Jun 2010 04:20:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://blogsearch.google.com/ping/RPC2"/><atom:link rel="hub" href="http://blog.yodao.com/ping/RPC2"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://www.xianguo.com/xmlrpc/ping.php"/><atom:link rel="hub" href="http://www.zhuaxia.com/rpc/server.php"/><atom:link rel="hub" href="http://rpc.technorati.com/rpc/ping"/><atom:link rel="hub" href="http://rpc.pingomatic.com/"/>	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://iregex.org/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>在Excel VBA中使用正则表达式</title>
		<link>http://iregex.org/blog/regex-in-excel-vba.html</link>
		<comments>http://iregex.org/blog/regex-in-excel-vba.html#comments</comments>
		<pubDate>Thu, 25 Dec 2008 03:24:32 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[教程]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=43</guid>
		<description><![CDATA[在Excel中使用正则式，需要借助于Excel自带的Visual Basic环境。我查了一下Microsoft Office Excel 2007 Formulas &#38; Functions FOR DUMmIES以及Excel VBA Programming For Dummies，两本书中均没有关于正则表达式的函数，... ]]></description>
			<content:encoded><![CDATA[<p><a title="在Excel中使用正则表达式|题图|我爱正则表达式" target="_blank" href="http://iregex.org/blog/regex-in-excel-vba.html"><img border="0" alt="在Excel中使用正则表达式|题图|我爱正则表达式" src="http://i293.photobucket.com/albums/mm60/zhasm/regex_in_excel.png" /></a></p>
<p>在Excel中使用<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>，需要借助于Excel自带的Visual Basic环境。我查了一下Microsoft Office Excel 2007 Formulas &amp; Functions FOR DUMmIES以及Excel VBA Programming For Dummies，两本书中均没有关于<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>的函数，也没有关于如何在VB中使用<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>的介绍。不过，我还是在google中使用excel 和regex 作为关键词找到了答案，总结在这里。我没怎么使用过VB，这里的代码只是浅尝辄止式，能够运行而已。本文假设读者有实际操作Excel VBA的经验。</p>
<p><span id="more-43"></span></p>
<p><strong>1. 创建按钮</strong></p>
<p>在Excel中可以方便地加入按钮，方法是右键点击菜单栏空白处，选中Visual Basic选项，在弹出的VB工具栏中点击<strong>控件工具箱</strong>，然后点击其中的“命令按钮”，再在当前工作表中合适位置拖动，即可创建一个按钮。如图所示。</p>
<ul>
<li><a title="我爱正则表达式|http://iregex.org" target="_blank" href="http://iregex.org/blog/regex-in-excel-vba.html"><img border="0" alt="我爱正则表达式|http://iregex.org" src="http://i293.photobucket.com/albums/mm60/zhasm/vb_menu.png" /></a> </li>
<li><a title="我爱正则表达式|http://iregex.org" target="_blank" href="http://iregex.org/blog/regex-in-excel-vba.html"><img border="0" alt="我爱正则表达式|http://iregex.org" src="http://i293.photobucket.com/albums/mm60/zhasm/vb_control.png" /></a> </li>
<li><a title="我爱正则表达式|http://iregex.org" target="_blank" href="http://iregex.org/blog/regex-in-excel-vba.html"><img border="0" alt="我爱正则表达式|http://iregex.org" src="http://i293.photobucket.com/albums/mm60/zhasm/vb_excel.png" /></a> </li>
</ul>
<p><strong>2. 代码框架：将该按钮与VB代码相关联。</strong></p>
<p>在菜单-&gt;宏-&gt;Visual Basic 编辑器中，为CommandButton1_Click()添加代码如下：</p>
<div class="codecolorer-container vb mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CommandButton1_Click()<br />
<span style="color: #000080;">Dim</span> RegEx &nbsp; &nbsp; &nbsp;<span style="color: #000080;">As</span> <span style="color: #000080;">Object</span><br />
<span style="color: #000080;">Dim</span> Myrange <span style="color: #000080;">As</span> Range, C <span style="color: #000080;">As</span> Range<br />
<br />
&nbsp; &nbsp; <span style="color: #008000;">' 将RegEx设置为vbscript.regexp正则对象<br />
</span> &nbsp; &nbsp;<span style="color: #000080;">Set</span> RegEx = CreateObject(<span style="color: #800000;">&quot;vbscript.regexp&quot;</span>)<br />
<br />
&nbsp; &nbsp; <span style="color: #008000;">' 设置RegExp 的属性<br />
</span> &nbsp; &nbsp;<span style="color: #000080;">With</span> RegEx<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'look for global matches 全局匹配<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;.Global = <span style="color: #000080;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">' 选择非数字的文本<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;.Pattern = <span style="color: #800000;">&quot;[^\d]+&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">With</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000;">' 将当前活动工作表中的所有批注赋值给cmt<br />
</span> &nbsp; &nbsp;<span style="color: #000080;">Set</span> cmt = ActiveSheet.Comments<br />
&nbsp; &nbsp; <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> xC <span style="color: #000080;">In</span> cmt<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">'对于每一条批注,删除其中的非数字文本,只留下数字文本<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;xC.Text RegEx.Replace(xC.Text, <span style="color: #800000;">&quot;&quot;</span>)<br />
&nbsp; &nbsp; <span style="color: #000080;">Next</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> Myrange = <span style="color: #000080;">Nothing</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> RegEx = <span style="color: #000080;">Nothing</span><br />
<br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></td></tr></tbody></table></div>
<p>上面代码的作用是将当前活动工作表中所有的批注中的非数字内容删除。例如，如果某单元格的批注为“ab1234cd” ，则执行代码后，其结果是“1234”。</p>
<p>回到工作表中，新建几条同时含数字和非数字内容的批注，执行代码，则其中的非数字项会被删除。</p>
<p><strong>3. 分析代码</strong></p>
<p>步骤2展示的是如何在Excel中调用<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>来处理文本。总结一下，其过程如下：</p>
<ol>
<li>声明<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>对象； </li>
<li>赋值给该对象，包括全局性、大小写模式、<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>内容； </li>
<li>执行<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>。 </li>
</ol>
<p>执行<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>时，我们有3种函数可用：Test、Execute、Replace。</p>
<ul>
<li>Test：检测目标文本与<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>是否匹配，返回True or False。 </li>
<li>Execute：检测是否匹配，并返回匹配集合，供后续程序调用处理。 </li>
<li>Replace：执行替换操作。在替换时，可以使用$1,$2等捕获变量，与常规的<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>意义相同，不赘述。 </li>
</ul>
<p><strong>4. 应用举例</strong></p>
<p>单纯的<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>应用很简单，难点在于在不同的场合和环境中，结合目标语言，正确使用它。在Excel中使用<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则式</a>，我认为应该特别注意的地方是Excel的独特的变量、活动表格、单元格范围，诸如此类。不过，这些内容应该可以通过查询帮助文档来获得。文末附了几本与Excel、VB编程相关的书籍，可供参考。</p>
<p>这里再举一则Execute函数的例子。</p>
<div class="codecolorer-container vb mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CommandButton2_Click()<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> myRegExp <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> Myrange <span style="color: #000080;">As</span> Range, C <span style="color: #000080;">As</span> Range<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> myRegExp = CreateObject(<span style="color: #800000;">&quot;vbscript.regexp&quot;</span>)<br />
&nbsp; &nbsp; myRegExp.Pattern = <span style="color: #800000;">&quot;(\d{4})(\w{2})&quot;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> Myrange = ActiveSheet.Range(<span style="color: #800000;">&quot;B3:B7&quot;</span>)<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> C <span style="color: #000080;">In</span> Myrange<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Set</span> myMatches = myRegExp.Execute(C.Value)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> myMatches.Count &gt;= 1 <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Set</span> myMatch = myMatches(0)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> myMatch.SubMatches.Count &gt;= 2 <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox myMatch.SubMatches(0)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'第一组匹配，(\d{4})部分<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MsgBox myMatch.SubMatches(1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'第二组匹配，(\w{2})部分<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Next</span><br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></td></tr></tbody></table></div>
<p>本程序的作用是将当前活动工作表中B3:B12范围单元格内的连续4位的数字取出来，使用MsgBox形式输出。执行结果为：</p>
<ol>
<li>B3单元格匹配，结果为2323、ac。 </li>
<li>B7单元格匹配，结果为3233、23。 </li>
</ol>
<p>文中的程序在MS Office 2003 Excel下通过。</p>
<p>附：与Excel、VBA、<a title="http://iregex.org|我爱正则表达式" target="_blank" href="http://iregex.org">正则表达式</a>相关的一些资源：</p>
<ul>
<li><a title="我爱正则表达式" target="_blank" href="http://qlh1bw.bay.livefilestore.com/y1pB0vUggWmzwXjN7Ve1mJKRY4H3fyAVbd2TuqBaMIzne-I_TE5YyIMsPXQVAfLCvC4kB_Aotuq1nL-_1UteoZvWw/RegExp%20Sept%201%202004.7z?download">RegExp Sept 1 2004.xls</a><sup><font color="#ff0080">荐！</font></sup>，一个200k的excel文档（英文），Excel下正则式运用之典范。运行时请将<font color="#ff00ff">工具-&gt;宏-&gt;安全性</font>调至<font color="#ff00ff">低</font>。 </li>
<li><a target="_blank" href="http://rapidshare.com/files/158521742/0470046554.zip">Excel 2007 Formulas &amp; Functions FOR DUMmIES</a> （386页英文PDF，9.16Mb，需登录该页面下载，下同。） </li>
<li><a target="_blank" href="http://rapidshare.com/files/154427001/0764574124.pdf">Excel VBA Programming For Dummies</a> （425页英文PDF，6.18Mb） </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/regex-in-excel-vba.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
