<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<title>正则表达式30分钟入门教程</title>
<meta name="keywords" content="正则表达式, 教程, 入门, 文本匹配, 语法" />
<meta name="description" content="30分钟内让你明白正则表达式是什么,并对它有一些基本的了解,让你可以在自己的程序或网页里使用它。" />
<meta name="author" content="deerchao" />
<meta name="author email" content="deerchao@gmail.com" />
<style type="text/css">
body {font-size: 9pt;font-family: Verdana, Arial, Helvetica, sans-serif;line-height:160%;}
h1 {text-align:center}
h2 {font-size: 12pt;line-height:140%;background-color:#eee; border-bottom:solid 1px gray; border-right:solid 1px gray; clear:both;}
p {text-indent:2em; line-height:140%; margin:5px 20px}
span {margin:3px}
table {margin:auto; border-style:solid; border-width:1px 1px 0 0; border-color:gray; background:#eee;}
td, th {border-style:solid; border-width:0 0 1px 1px; border-color:gray}
caption {margin:auto; font-weight:bold}
dl {margin-left:20px}
dt {font-weight:bold}
em {font-style:normal; font-weight:bold; }
.webStandards {text-align:right; font-size:80%; color: gray;}
.webStandards a {text-align:right; font-size:small; color: gray;}
#ad {margin-top:60px;}
.ad { border: solid 1px #eee; margin-bottom:60px; clear:right; }
.note { float:right; width:300px; padding:5px; background-color:#eee; border:solid 1px gray; clear:right;}
#meta {text-align: center}
.important {color:orange;}
.name {font-weight:bold}
.code {color:blue}
.regex {color:red}
.part {color:green}
.string {font-style:italic}
.desc {text-decoration:underline}
#clearButton,#hideButton {text-decoration:none; border:solid 1px gray; background-color:#eee;}
.STYLE2 {
color: #993300;
font-size: 16pt;
font-weight: bold;
}
</style>
</head>
<body>
<div align="center"><br />
<span class="STYLE2">正则表达式30分钟入门教程</span>
</div>
<p id="meta">版本:v2.31 (2009-4-11) 作者:<a href="http://deerchao.net/">deerchao</a> <a href="http://deerchao.net/tutorials/regex/regex.htm">来源</a></p>
<h2 id="contents">目录</h2>
<p id="skipContents" class="note"><a href="#mission" title="转到正文内容">跳过目录</a></p>
<ol>
<li><a href="#mission">本文目标</a></li>
<li><a href="#howtouse">如何使用本教程</a></li>
<li><a href="#introduction">正则表达式到底是什么东西?</a></li>
<li><a href="#getstarted">入门</a></li>
<li><a href="#testing">测试正则表达式</a></li>
<li><a href="#metacode">元字符</a></li>
<li><a href="#escape">字符转义</a></li>
<li><a href="#repeat">重复</a></li>
<li><a href="#charclass">字符类</a></li>
<li><a href="#alternative">分支条件</a></li>
<li><a href="#negation">反义</a></li>
<li><a href="#grouping">分组</a></li>
<li><a href="#backreference">后向引用</a></li>
<li><a href="#lookaround">零宽断言</a></li>
<li><a href="#negativelookaround">负向零宽断言</a></li>
<li><a href="#commenting">注释</a></li>
<li><a href="#greedyandlazy">贪婪与懒惰</a></li>
<li><a href="#regexoptions">处理选项</a></li>
<li><a href="#balancedgroup">平衡组/递归匹配</a></li>
<li><a href="#more">还有些什么东西没提到</a></li>
<li><a href="#contact">联系作者</a></li>
<li><a href="#ad">最后,来点广告……</a></li>
<li><a href="#resources">网上的资源及本文参考文献</a></li>
<li><a href="#updatelog">更新纪录</a></li>
</ol>
<h2 id="mission">本文目标</h2>
<p>30分钟内让你明白正则表达式是什么,并对它有一些基本的了解,让你可以在自己的程序或网页里使用它。</p>
<h2 id="howtouse">如何使用本教程</h2>
<p id="giveMe30Minutes" class="important note">最重要的是——请给我<em>30分钟</em>,如果你没有使用正则表达式的经验,请不要试图在30<em>秒</em>内入门——除非你是超人 :)</p>
<p>别被下面那些复杂的表达式吓倒,只要跟着我一步一步来,你会发现正则表达式其实并<span lang="zh-cn">没有</span>你想像中的那么困难。当然,如果你看完了这篇教程之后,发现自己明白了很多,却又几乎什么都记不得,那也是很正常的——我认为,没接触过正则表达式的人在看完这篇教程后,能把提到过的语法记住80%以上的可能性为零。这里只是让你明白基本的原理,以后你还需要多练习,多使用,才能熟练掌握正则表达式。</p>
<p>除了作为入门教程之外,本文还试图成为可以在日常工作中使用的正则表达式语法参考手册。就作者本人的经历来说,这个目标还是完成得不错的——你看,我自己也没能把所有的东西记下来,不是吗?</p>
<p><a href="http://deerchao.net/tutorials/regex/regex.htm" id="clearButton" onclick="return clearFormats();">清除格式</a> 文本格式约定:<span class="name">专业术语</span> <span class="code">元字符/语法格式</span> <span class="regex">正则表达式</span> <span class="part">正则表达式中的一部分(用于分析)</span> <span class="string">对其进行匹配的源字符串</span> <span class="desc">对正则表达式或其中一部分的说明</span></p>
<p><a href="http://deerchao.net/tutorials/regex/regex.htm" id="hideButton" onclick="return hideNotes();">隐藏边注</a> 本文右边有一些注释,主要是用来提供一些相关信息,或者给没有程序员背景的读者解释一些基本概念,通常可以忽略。</p>
<h2 id="introduction">正则表达式到底是什么东西?</h2>
<p class="note"><span class="name">字符</span>是计算机软件处理文字时最基本的单位,可能是字母,数字,标点符号,空格,换行符,汉字等等。<span class="name">字符串</span>是0个或更多个字符的序列。<span class="name">文本</span>也就是文字,字符串。说某个字符串<span class="name">匹配</span>某个正则表达式,通常是指这个字符串里有一部分(或几部分分别)能满足表达式给出的条件。</p>
<p>在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要。<span class="name">正则表达式</span>就是用于描述这些规则的工具。换句话说,正则表达式就是记录文本规则的代码。</p>
<p>很可能你使用过Windows/Dos下用于文件查找的<span class="name">通配符(wildcard)</span>,也就是<span class="code">*</span>和<span class="code">?</span>。如果你想查找某个目录下的所有的Word文档的话,你会搜索<span style="color: red;">*.doc</span>。在这里,<span class="code">*</span>会被解释成任意的字符串。和通配符类似,正则表达式也是用来进行文本匹配的工具,只不过比起通配符,它能更精确地描述你的需求——当然,代价就是更复杂——比如你可以编写一个正则表达式,用来查找<span class="desc">所有以0�