??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲熟妇AV一区二区三区浪潮 ,亚洲国产无套无码av电影,亚洲人成电影青青在线播放http://www.tkk7.com/masen/category/20993.htmlzh-cnMon, 20 Aug 2007 18:26:12 GMTMon, 20 Aug 2007 18:26:12 GMT60javascript正则表达式对?/title><link>http://www.tkk7.com/masen/articles/138106.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 20 Aug 2007 05:20:00 GMT</pubDate><guid>http://www.tkk7.com/masen/articles/138106.html</guid><wfw:comment>http://www.tkk7.com/masen/comments/138106.html</wfw:comment><comments>http://www.tkk7.com/masen/articles/138106.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/masen/comments/commentRss/138106.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/masen/services/trackbacks/138106.html</trackback:ping><description><![CDATA[<p><br>本对象包含正则表辑ּ模式以及表明如何应用模式的标志?br>语法 1 re = /pattern/[flags]<br>语法 2 re = new RegExp("pattern",["flags"]) <br>参数<br>re<br>必选项。将要赋gؓ正则表达式模式的变量名?<br>Pattern<br>必选项。要使用的正则表辑ּ模式。如果用语?1Q用 "/" 字符分隔模式。如果用语法 2Q用引号模式引h?</p> <p>Flags <br>可选项。如果用语?2 要用引号?flag 引v来。标志可以组合用,可用的有Q?<br>g Q全文查扑և现的所?patternQ?<br>i Q忽略大写Q?<br>m Q多行查找) <br>CZ<br>下面的示例创Z个包含正则表辑ּ模式及相x志的对象(re)Q向您演C正则表辑ּ对象的用法。在本例中,作ؓl果的正则表辑ּ对象又用?match Ҏ中:<br>function MatchDemo()<br>{<br>var r, re; // 声明变量?br>var s = "The rain in Spain falls mainly in the plain";<br>re = new RegExp("ain","g"); // 创徏正则表达式对象?br>r = s.match(re); // 在字W串 s 中查扑֌配?br>return(r); <br>}<br>q回| ain,ain,ain,ain<br>属?lastIndex 属?| source 属?br>Ҏ compile Ҏ | exec Ҏ | test Ҏ<br>要求 版本 3<br>请参?RegExp 对象 | 正则表达式语?| String 对象</p> <p>exec Ҏ</p> <p><br>用正则表辑ּ模式在字W串中运行查找,q返回包含该查找l果的一个数l?br>rgExp.exec(str)<br>参数<br>rgExp <br>必选项。包含正则表辑ּ模式和可用标志的正则表达式对象?<br>str <br>必选项。要在其中执行查扄 String 对象或字W串文字?<br>说明<br>??exec Ҏ没有扑ֈ匚wQ则它返?null。如果它扑ֈ匚wQ则 exec Ҏq回一个数l,q且更新全局 RegExp 对象的属性,以反映匹配结果。数l的0元素包含了完整的匚wQ而第1到n元素中包含的是匹配中出现的Q意一个子匚w。这相当于没有设|全局标志 (g) ?match Ҏ?br>如果为正则表辑ּ讄了全局标志Qexec 从以 lastIndex 的值指C的位置开始查找。如果没有设|全局标志Qexec 忽略 lastIndex 的|从字W串的v始位|开始搜索?br>exec Ҏq回的数l有三个属性,分别?input、index ?lastIndex。Input 属性包含了整个被查扄字符丌ӀIndex 属性中包含了整个被查找字符串中被匹配的子字W串的位|。LastIndex 属性中包含了匹配中最后一个字W的下一个位|?br>CZ<br>下面的例子D例说明了 exec Ҏ的用法:<br>function RegExpTest()<br>{<br>var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())<br>if (ver >= 5.5){ // 试 JScript 的版本?br>var src = "The rain in Spain falls mainly in the plain.";<br>var re = /\w+/g; // 创徏正则表达式模式?br>var arr;<br>while ((arr = re.exec(src)) != null)<br>document.write(arr.index + "-" + arr.lastIndex + arr + "\t");<br>}<br>else{<br>alert("请?JScript 的更新版?);<br>}<br>}<br>q回|0-3The 4-8rain 9-11in 12-17Spain 18-23falls 24-30mainly 31-33in 34-37the 38-43plain</p> <p><br>test Ҏ</p> <p><br>q回一?Boolean |它指出在被查扄字符串中是否存在模式?br>rgexp.test(str) <br>参数<br>rgexp<br>必选项。包含正则表辑ּ模式或可用标志的正则表达式对象?<br>str <br>必选项。要在其上测试查扄字符丌Ӏ?<br>说明<br>test Ҏ查在字符串中是否存在一个模式,如果存在则返?trueQ否则就q回 false?br>全局 RegExp 对象的属性不?test Ҏ来修攏V?br>CZ<br>下面的例子D例说明了 test Ҏ的用法:<br>function TestDemo(re, s)<br>{<br>var s1; // 声明变量?br>// 查字W串是否存在正则表达式?br>if (re.test(s)) // 试是否存在?br>s1 = " contains "; // s 包含模式?br>else<br>s1 = " does not contain "; // s 不包含模式?br>return("'" + s + "'" + s1 + "'"+ re.source + "'"); // q回字符丌Ӏ?br>}<br>函数调用Qdocument.write (TestDemo(/ain+/ ,"The rain in Spain falls mainly in the plain."));<br>q回|'The rain in Spain falls mainly in the plain.' contains 'ain+'</p> <p><br>match Ҏ</p> <p><br>使用正则表达式模式对字符串执行查找,q将包含查找的结果作为数l返回?br>stringObj.match(rgExp) <br>参数<br>stringObj <br>必选项。对其进行查扄 String 对象或字W串文字?<br>rgExp <br>必选项。ؓ包含正则表达式模式和可用标志的正则表辑ּ对象。也可以是包含正则表辑ּ模式和可用标志的变量名或字符串文字?<br>说明<br>如果 match Ҏ没有扑ֈ匚wQ返?null。如果找到匹配返回一个数lƈ且更新全局 RegExp 对象的属性以反映匚wl果?br>match Ҏq回的数l有三个属性:input、index ?lastIndex。Input 属性包含整个的被查扑֭W串。Index 属性包含了在整个被查找字符串中匚w的子字符串的位置。LastIndex 属性包含了最后一ơ匹配中最后一个字W的下一个位|?br>如果没有讄全局标志 (g)Q数l的 0 元素包含整个匚wQ而第 1 ?n 元素包含了匹配中曑և现过的Q一个子匚w。这相当于没有设|全局标志?exec Ҏ。如果设|了全局标志Q元?0 ?n 中包含所有匹配?br>CZ<br>下面的示例演CZmatch Ҏ的用法:<br>function MatchDemo()<br>{<br>var r, re; // 声明变量?br>var s = "The rain in Spain falls mainly in the plain";<br>re = /ain/i; // 创徏正则表达式模式?br>r = s.match(re); // 试匚w搜烦字符丌Ӏ?br>return(r); // q回W一ơ出?"ain" 的地斏V?br>}<br>q回|ain<br>本示例说明带 g 标志讄?match Ҏ的用法?br>function MatchDemo()<br>{<br>var r, re; // 声明变量?br>var s = "The rain in Spain falls mainly in the plain";<br>re = /ain/ig; // 创徏正则表达式模式?br>r = s.match(re); // 试d配搜索字W串?br>return(r); // q回的数l包含了所?"ain" <br>// 出现的四个匹配?br>}<br>q回|ain,ain,ain,ain<br>上面几行代码演示了字W串文字?match Ҏ的用法?br>var r, re = "Spain";<br>r = "The rain in Spain".replace(re, "Canada");<br>return r;<br>q回|The rain in Canada</p> <p><br>search Ҏ</p> <p><br>q回与正则表辑ּ查找内容匚w的第一个子字符串的位置?br>stringObj.search(rgExp)<br>参数<br>stringObj <br>必选项。要在其上进行查扄 String 对象或字W串文字?<br>rgExp <br>必选项。包含正则表辑ּ模式和可用标志的正则表达式对象?<br>说明<br>search Ҏ指明是否存在相应的匹配。如果找C个匹配,search Ҏ返回一个整数|指明q个匚w距离字符串开始的偏移位置。如果没有找到匹配,则返?-1?br>CZ<br>下面的示例演CZ search Ҏ的用法?br>function SearchDemo()<br>{<br>var r, re; // 声明变量?br>var s = "The rain in Spain falls mainly in the plain.";<br>re = /falls/i; // 创徏正则表达式模式?br>r = s.search(re); // 查找字符丌Ӏ?br>return(r); // q回 Boolean l果?br>}<br>q回|18</p> <p><br>                <br> </p> <p>     <br></p> <img src ="http://www.tkk7.com/masen/aggbug/138106.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/masen/" target="_blank">Masen</a> 2007-08-20 13:20 <a href="http://www.tkk7.com/masen/articles/138106.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>验证日期正则表达?/title><link>http://www.tkk7.com/masen/articles/138105.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 20 Aug 2007 05:18:00 GMT</pubDate><guid>http://www.tkk7.com/masen/articles/138105.html</guid><wfw:comment>http://www.tkk7.com/masen/comments/138105.html</wfw:comment><comments>http://www.tkk7.com/masen/articles/138105.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/masen/comments/commentRss/138105.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/masen/services/trackbacks/138105.html</trackback:ping><description><![CDATA[q里是判断yyyy-mm-ddq种格式的,基本上把闰年?月等的情况都考虑q去了,不过我已l忘了在哪里扑ֈ的?<br><br>^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$ <br><br><br><br>下面的是加了旉验证?<br><br>^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$ <br> <img src ="http://www.tkk7.com/masen/aggbug/138105.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/masen/" target="_blank">Masen</a> 2007-08-20 13:18 <a href="http://www.tkk7.com/masen/articles/138105.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>正则表达式大?/title><link>http://www.tkk7.com/masen/articles/123365.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Mon, 11 Jun 2007 04:59:00 GMT</pubDate><guid>http://www.tkk7.com/masen/articles/123365.html</guid><wfw:comment>http://www.tkk7.com/masen/comments/123365.html</wfw:comment><comments>http://www.tkk7.com/masen/articles/123365.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/masen/comments/commentRss/123365.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/masen/services/trackbacks/123365.html</trackback:ping><description><![CDATA[<p><strong>前言</strong></p> <p>正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真去阅读q些资料Q加上应用的时候进行一定的参考,掌握正则表达式不是问题?/p> <p><strong>索引</strong></p> <p><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#1._引子" target=_blank rel=nofollow><font color=#0000ff size=2><u>1._引子</u></font></a><font size=2><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#2._正则表达式的历史" target=_blank rel=nofollow><font color=#0000ff><u>2._正则表达式的历史</u></font></a><br></font><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3._正则表达式定? target=_blank rel=nofollow><font color=#0000ff size=2><u>3._正则表达式定?/u></font></a></p> <blockquote> <p><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.1_普通字W? target=_blank rel=nofollow><font color=#0000ff size=2><u>3.1_普通字W?/u></font></a><font size=2><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.2_非打印字W? target=_blank rel=nofollow><font color=#0000ff><u>3.2_非打印字W?/u></font></a><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.3_Ҏ字符" target=_blank rel=nofollow><font color=#0000ff><u>3.3_Ҏ字符</u></font></a><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.4_限定W? target=_blank rel=nofollow><font color=#0000ff><u>3.4_限定W?/u></font></a><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.5_定位W? target=_blank rel=nofollow><font color=#0000ff><u>3.5_定位W?/u></font></a><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.6_选择" target=_blank rel=nofollow><font color=#0000ff><u>3.6_选择</u></font></a><br></font><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#3.7_后向引用" target=_blank rel=nofollow><font color=#0000ff size=2><u>3.7_后向引用</u></font></a></p> </blockquote> <p><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#4._各种操作W的q算优先U? target=_blank rel=nofollow><font color=#0000ff size=2><u>4._各种操作W的q算优先U?/u></font></a><font size=2><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#5._全部W号解释" target=_blank rel=nofollow><font color=#0000ff><u>5._全部W号解释</u></font></a><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#6._部分例子" target=_blank rel=nofollow><font color=#0000ff><u>6._部分例子</u></font></a><br></font><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#7._正则表达式匹配规? target=_blank rel=nofollow><font color=#0000ff size=2><u>7._正则表达式匹配规?/u></font></a></p> <blockquote> <p><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#7.1_基本模式匚w" target=_blank rel=nofollow><font color=#0000ff size=2><u>7.1_基本模式匚w</u></font></a><font size=2><br><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#7.2_字符? target=_blank rel=nofollow><font color=#0000ff><u>7.2_字符?/u></font></a><br></font><a href="file:///D:/LinkToneCodes/Tone/ToneRobot/正则表达?regular%20expression).htm#7.3_定重复出现" target=_blank rel=nofollow><font color=#0000ff size=2><u>7.3_定重复出现</u></font></a></p> </blockquote><u><font color=#0000ff> <hr> </font></u> <h2><a target=_blank rel=nofollow name=1._引子>1. 引子</a></h2> <p>  目前Q正则表辑ּ已经在很多Y件中得到q泛的应用,包括*nixQLinux, Unix{)QHP{操作系l,PHPQC#QJava{开发环境,以及很多的应用Y件中Q都可以看到正则表达式的影子?/p> <p>  正则表达式的使用Q可以通过单的办法来实现强大的功能。ؓ了简单有效而又不失强大Q造成了正则表辑ּ代码的难度较大,学习h也不是很ҎQ所以需要付Z些努力才行,入门之后参照一定的参考,使用hq是比较单有效的?/p> <blockquote> <p>例子Q?span style="BACKGROUND-COLOR: #00ffff"> ^.+@.+\\..+$ </span></p> </blockquote> <p>  q样的代码曾l多ơ把我自q吓退q。可能很多h也是被这L代码l吓跑的吧。l阅L文将让你也可以自由应用这L代码?/p> <p>  注意Q这里的W?部分跟前面的内容看v来似乎有些重复,目的是把前面表格里的部分重新描述了一ơ,目的是让q些内容更容易理解?/p> <h2><strong><a target=_blank rel=nofollow name=2._正则表达式的历史>2. 正则表达式的历史</a></strong></h2> <div class="3hbj19l" id=nstext valign="bottom">  正则表达式的“先”可以一直上溯至对hcȝl系l如何工作的早期研究。Warren McCulloch ?Walter Pitts q两位神l生理学家研I出一U数学方式来描述q些经|络? <p>  1956 q? 一位叫 Stephen Kleene 的数学家?McCulloch ?Pitts 早期工作的基上,发表了一标题ؓ“经|事件的表示?#8221;的论文,引入了正则表辑ּ的概c正则表辑ּ是用来描述他称?#8220;正则集的代数”的表辑ּQ因此采?#8220;正则表达?#8221;q个术语?/p> <p>  随后Q发现可以将q一工作应用于?Ken Thompson 的计搜索算法的一些早期研IӞKen Thompson ?Unix 的主要发明h。正则表辑ּ的第一个实用应用程序就?Unix 中的 <em>qed </em>~辑器?/p> <p>  如他们所_剩下的就是众所周知的历史了。从那时L至现在正则表辑ּ都是Z文本的编辑器和搜索工具中的一个重要部分?/p> </div> <p> </p> <h2><strong><a target=_blank rel=nofollow name=3._正则表达式定?3. 正则表达式定?/a></strong></h2> <p>  正则表达?regular expression)描述了一U字W串匚w的模式,可以用来查一个串是否含有某种子串、将匚w的子串做替换或者从某个串中取出W合某个条g的子串等?/p> <blockquote> <ul> <li><span style="BACKGROUND-COLOR: #ffff00">列目录时Q dir *.txt或ls *.txt中的*.txt?/span><font color=#ff0000><span style="BACKGROUND-COLOR: #ffff00">?/span></font><span style="BACKGROUND-COLOR: #ffff00">是一个正则表辑ּ,因ؓq里*与正则式?的含义是不同的?/span> </li> </ul> </blockquote> <p>  正则表达式是由普通字W(例如字符 a ?zQ以及特D字W(UCؓ元字W)l成的文字模式。正则表辑ּ作ؓ一个模板,某个字W模式与所搜烦的字W串q行匚w?/p> <h3><strong><a target=_blank rel=nofollow name=3.1_普通字W?3.1 普通字W?/a></strong></h3> <p>  由所有那些未昑ּ指定为元字符的打印和非打印字W组成。这包括所有的大写和小写字母字W,所有数字,所有标点符号以及一些符受?</p> <h3><a target=_blank rel=nofollow name=3.2_非打印字W?3.2 非打印字W?/a></h3> <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>字符 </th> <th style="FONT-SIZE: 12px" align=left>含义</th> </tr> <tr> <td style="FONT-SIZE: 12px">\cx </td> <td style="FONT-SIZE: 12px">匚w由x指明的控制字W。例如, \cM 匚w一?Control-M 或回车符。x 的值必Mؓ A-Z ?a-z 之一。否则,?c 视ؓ一个原义的 'c' 字符?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\f </td> <td style="FONT-SIZE: 12px">匚w一个换늬。等价于 \x0c ?\cL?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\n </td> <td style="FONT-SIZE: 12px">匚w一个换行符。等价于 \x0a ?\cJ?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\r </td> <td style="FONT-SIZE: 12px">匚w一个回车符。等价于 \x0d ?\cM?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\s </td> <td style="FONT-SIZE: 12px">匚wMI白字符Q包括空根{制表符、换늬{等。等价于 [ \f\n\r\t\v]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\S </td> <td style="FONT-SIZE: 12px">匚wM非空白字W。等价于 [^ \f\n\r\t\v]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\t </td> <td style="FONT-SIZE: 12px">匚w一个制表符。等价于 \x09 ?\cI?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\v </td> <td style="FONT-SIZE: 12px">匚w一个垂直制表符。等价于 \x0b ?\cK?/td> </tr> </tbody> </table> <br>  <h3><a target=_blank rel=nofollow name=3.3_Ҏ字符>3.3 Ҏ字符</a></h3>   所谓特D字W,是一些有Ҏ含义的字W,如上面说?*.txt"中的*Q简单的说就是表CZQ何字W串的意思。如果要查找文g名中有*的文Ӟ则需要对Q进行{义,卛_其前加一个\。ls \*.txt。正则表辑ּ有以下特D字W? <p> </p> <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>特别字符</th> <th style="FONT-SIZE: 12px" align=left>说明</th> </tr> <tr> <td style="FONT-SIZE: 12px">$</td> <td style="FONT-SIZE: 12px">匚w输入字符串的l尾位置。如果设|了 RegExp 对象?Multiline 属性,?$ 也匹?'\n' ?'\r'。要匚w $ 字符本nQ请使用 \$?/td> </tr> <tr> <td style="FONT-SIZE: 12px">( )</td> <td style="FONT-SIZE: 12px">标记一个子表达式的开始和l束位置。子表达式可以获取供以后使用。要匚wq些字符Q请使用 \( ?\)?/td> </tr> <tr> <td style="FONT-SIZE: 12px">*</td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或多次。要匚w * 字符Q请使用 \*?/td> </tr> <tr> <td style="FONT-SIZE: 12px">+</td> <td style="FONT-SIZE: 12px">匚w前面的子表达式一ơ或多次。要匚w + 字符Q请使用 \+?/td> </tr> <tr> <td style="FONT-SIZE: 12px">.</td> <td style="FONT-SIZE: 12px">匚w除换行符 \n之外的Q何单字符。要匚w .Q请使用 \?/td> </tr> <tr> <td style="FONT-SIZE: 12px">[ </td> <td style="FONT-SIZE: 12px">标记一个中括号表达式的开始。要匚w [Q请使用 \[?/td> </tr> <tr> <td style="FONT-SIZE: 12px">?</td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或一ơ,或指明一个非贪婪限定W。要匚w ? 字符Q请使用 \??/td> </tr> <tr> <td style="FONT-SIZE: 12px">\</td> <td style="FONT-SIZE: 12px">下一个字W标Cؓ或特D字W、或原义字符、或向后引用、或八进制{义符。例如, 'n' 匚w字符 'n'?\n' 匚w换行W。序?'\\' 匚w "\"Q?'\(' 则匹?"("?/td> </tr> <tr> <td style="FONT-SIZE: 12px">^</td> <td style="FONT-SIZE: 12px">匚w输入字符串的开始位|,除非在方括号表达式中使用Q此时它表示不接受该字符集合。要匚w ^ 字符本nQ请使用 \^?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{</td> <td style="FONT-SIZE: 12px">标记限定W表辑ּ的开始。要匚w {Q请使用 \{?/td> </tr> <tr> <td style="FONT-SIZE: 12px">|</td> <td style="FONT-SIZE: 12px">指明两项之间的一个选择。要匚w |Q请使用 \|?/td> </tr> </tbody> </table> <blockquote> <ul> <li><strong style="FONT-WEIGHT: 400; BACKGROUND-COLOR: #ffff00">  构造正则表辑ּ的方法和创徏数学表达式的Ҏ一栗也是用多U元字符与操作符小的表辑ּl合在一h创徏更大的表辑ּ。正则表辑ּ的组件可以是单个的字W、字W集合、字W范围、字W间的选择或者所有这些组件的Ll合?/strong> </li> </ul> </blockquote> <p> </p> <h3><a target=_blank rel=nofollow name=3.4_限定W?3.4 限定W?/a></h3>   限定W用来指定正则表辑ּ的一个给定组件必要出现多少ơ才能满_配。有*??或{n}或{n,}或{n,m}?U?br>*??限定W都是贪婪的Q因为它们会可能多的匹配文字,只有在它们的后面加上一?可以实现非贪婪或最匹配?br>  正则表达式的限定W有Q?br>  <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>字符 </th> <th style="FONT-SIZE: 12px" align=left>描述</th> </tr> <tr> <td style="FONT-SIZE: 12px">* </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或多次。例如,zo* 能匹?"z" 以及 "zoo"? {h于{0,}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">+ </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式一ơ或多次。例如,'zo+' 能匹?"zo" 以及 "zoo"Q但不能匚w "z"? {h?{1,}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">? </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或一ơ。例如,"do(es)?" 可以匚w "do" ?"does" 中的"do" ? {h?{0,1}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n} </td> <td style="FONT-SIZE: 12px">n 是一个非负整数。匹配确定的 n ơ。例如,'o{2}' 不能匚w "Bob" 中的 'o'Q但是能匚w "food" 中的两个 o?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n,} </td> <td style="FONT-SIZE: 12px">n 是一个非负整数。至匹配n ơ。例如,'o{2,}' 不能匚w "Bob" 中的 'o'Q但能匹?"foooood" 中的所?o?o{1,}' {h?'o+'?o{0,}' 则等价于 'o*'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n,m} </td> <td style="FONT-SIZE: 12px">m ?n 均ؓ非负整数Q其中n <= m。最匹?n ơ且最多匹?m ơ。例如,"o{1,3}" 匹?"fooooood" 中的前三?o?o{0,1}' {h?'o?'。请注意在逗号和两个数之间不能有空根{?/td> </tr> </tbody> </table> <h3><a target=_blank rel=nofollow name=3.5_定位W?3.5 定位W?/a></h3>   用来描述字符串或单词的边界,^?分别指字W串的开始与l束Q\b描述单词的前或后边界Q\B表示非单词边界?font color=#ff0000>不能对定位符使用限定W?/font> <h3><a target=_blank rel=nofollow name=3.6_选择>3.6 选择</a></h3>   用圆括号所有选择ҎhQ相ȝ选择之间用|分隔。但用圆括号会有一个副作用Q是相关的匹配会被缓存,此时可用?:攑֜W一个选项前来消除q种副作用?br>  其中?:是非捕获元之一Q还有两个非捕获元是?=?!Q这两个q有更多的含义,前者ؓ正向预查Q在M开始匹配圆括号内的正则表达式模式的位置来匹配搜索字W串Q后者ؓ负向预查Q在M开始不匚w该正则表辑ּ模式的位|来匚w搜烦字符丌Ӏ? <h3><a target=_blank rel=nofollow name=3.7_后向引用>3.7 后向引用</a></h3>   对一个正则表辑ּ模式或部分模式两Ҏ加圆括号导致相兛_配存储到一个时缓冲区中,所捕获的每个子匚w都按照在正则表达式模式中从左臛_所遇到的内容存储。存储子匚w的缓冲区~号?1 开始,q箋~号直至最?99 个子表达式。每个缓冲区都可以?'\n' 讉KQ其?n Z个标识特定缓冲区的一位或两位十进制数?br>  可以使用非捕获元字符 '?:', '?=', or '?!' 来忽略对相关匚w的保存? <h2><a target=_blank rel=nofollow name=4._各种操作W的q算优先U?4. 各种操作W的q算优先U?/a></h2>   相同优先U的从左到右q行q算Q不同优先的运先高后低。各U操作符的优先从高C如下Q?br>  <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>操作W?</th> <th style="FONT-SIZE: 12px" align=left>描述</th> </tr> <tr> <td style="FONT-SIZE: 12px">\ </td> <td style="FONT-SIZE: 12px">转义W?/td> </tr> <tr> <td style="FONT-SIZE: 12px">(), (?:), (?=), [] </td> <td style="FONT-SIZE: 12px">圆括号和Ҏ?/td> </tr> <tr> <td style="FONT-SIZE: 12px">*, +, ?, {n}, {n,}, {n,m} </td> <td style="FONT-SIZE: 12px">限定W?/td> </tr> <tr> <td style="FONT-SIZE: 12px">^, $, \anymetacharacter </td> <td style="FONT-SIZE: 12px">位置和顺?/td> </tr> <tr> <td style="FONT-SIZE: 12px">| </td> <td style="FONT-SIZE: 12px">“?#8221;操作</td> </tr> </tbody> </table> <h2><a target=_blank rel=nofollow name=5._全部W号解释>5. 全部W号解释</a></h2> <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>字符 </th> <th style="FONT-SIZE: 12px" align=left>描述</th> </tr> <tr> <td style="FONT-SIZE: 12px">\ </td> <td style="FONT-SIZE: 12px">下一个字W标Cؓ一个特D字W、或一个原义字W、或一?向后引用、或一个八q制转义W。例如,'n' 匚w字符 "n"?\n' 匚w一个换行符。序?'\\' 匚w "\" ?"\(" 则匹?"("?/td> </tr> <tr> <td style="FONT-SIZE: 12px">^ </td> <td style="FONT-SIZE: 12px">匚w输入字符串的开始位|。如果设|了 RegExp 对象?Multiline 属性,^ 也匹?'\n' ?'\r' 之后的位|?/td> </tr> <tr> <td style="FONT-SIZE: 12px">$ </td> <td style="FONT-SIZE: 12px">匚w输入字符串的l束位置。如果设|了RegExp 对象?Multiline 属性,$ 也匹?'\n' ?'\r' 之前的位|?/td> </tr> <tr> <td style="FONT-SIZE: 12px">* </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或多次。例如,zo* 能匹?"z" 以及 "zoo"? {h于{0,}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">+ </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式一ơ或多次。例如,'zo+' 能匹?"zo" 以及 "zoo"Q但不能匚w "z"? {h?{1,}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">? </td> <td style="FONT-SIZE: 12px">匚w前面的子表达式零ơ或一ơ。例如,"do(es)?" 可以匚w "do" ?"does" 中的"do" ? {h?{0,1}?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n} </td> <td style="FONT-SIZE: 12px">n 是一个非负整数。匹配确定的 n ơ。例如,'o{2}' 不能匚w "Bob" 中的 'o'Q但是能匚w "food" 中的两个 o?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n,} </td> <td style="FONT-SIZE: 12px">n 是一个非负整数。至匹配n ơ。例如,'o{2,}' 不能匚w "Bob" 中的 'o'Q但能匹?"foooood" 中的所?o?o{1,}' {h?'o+'?o{0,}' 则等价于 'o*'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">{n,m} </td> <td style="FONT-SIZE: 12px">m ?n 均ؓ非负整数Q其中n <= m。最匹?n ơ且最多匹?m ơ。例如,"o{1,3}" 匹?"fooooood" 中的前三?o?o{0,1}' {h?'o?'。请注意在逗号和两个数之间不能有空根{?/td> </tr> <tr> <td style="FONT-SIZE: 12px">? </td> <td style="FONT-SIZE: 12px">当该字符紧跟在Q何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面Ӟ匚w模式是非贪婪的。非贪婪模式可能少的匹配所搜烦的字W串Q而默认的贪婪模式则尽可能多的匚w所搜烦的字W串。例如,对于字符?"oooo"Q?o+?' 匹配单?"o"Q?'o+' 匹配所?'o'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">. </td> <td style="FONT-SIZE: 12px">匚w?"\n" 之外的Q何单个字W。要匚w包括 '\n' 在内的Q何字W,请用象 '[.\n]' 的模式?/td> </tr> <tr> <td style="FONT-SIZE: 12px">(pattern) </td> <td style="FONT-SIZE: 12px">匚w pattern q获取这一匚w。所获取的匹配可以从产生?Matches 集合得到Q在VBScript 中?SubMatches 集合Q在JScript 中则使用 $0…$9 属性。要匚w圆括号字W,请?'\(' ?'\)'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">(?:pattern) </td> <td style="FONT-SIZE: 12px">匚w pattern 但不获取匚wl果Q也是说这是一个非获取匚wQ不q行存储供以后用。这在?"? 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 是一个比 'industry|industries' 更简略的表达式?/td> </tr> <tr> <td style="FONT-SIZE: 12px">(?=pattern) </td> <td style="FONT-SIZE: 12px">正向预查Q在M匚w pattern 的字W串开始处匚w查找字符丌Ӏ这是一个非获取匚wQ也是_该匹配不需要获取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹?"Windows 2000" 中的 "Windows" Q但不能匚w "Windows 3.1" 中的 "Windows"。预查不消耗字W,也就是说Q在一个匹配发生后Q在最后一ơ匹配之后立卛_始下一ơ匹配的搜烦Q而不是从包含预查的字W之后开始?/td> </tr> <tr> <td style="FONT-SIZE: 12px">(?!pattern) </td> <td style="FONT-SIZE: 12px">负向预查Q在M不匹?pattern 的字W串开始处匚w查找字符丌Ӏ这是一个非获取匚wQ也是_该匹配不需要获取供以后使用。例?Windows (?!95|98|NT|2000)' 能匹?"Windows 3.1" 中的 "Windows"Q但不能匚w "Windows 2000" 中的 "Windows"。预查不消耗字W,也就是说Q在一个匹配发生后Q在最后一ơ匹配之后立卛_始下一ơ匹配的搜烦Q而不是从包含预查的字W之后开?/td> </tr> <tr> <td style="FONT-SIZE: 12px">x|y </td> <td style="FONT-SIZE: 12px">匚w x ?y。例如,'z|food' 能匹?"z" ?"food"?(z|f)ood' 则匹?"zood" ?"food"?/td> </tr> <tr> <td style="FONT-SIZE: 12px">[xyz] </td> <td style="FONT-SIZE: 12px">字符集合。匹配所包含的Q意一个字W。例如, '[abc]' 可以匚w "plain" 中的 'a'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">[^xyz] </td> <td style="FONT-SIZE: 12px">负值字W集合。匹配未包含的Q意字W。例如, '[^abc]' 可以匚w "plain" 中的'p'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">[a-z] </td> <td style="FONT-SIZE: 12px">字符范围。匹配指定范围内的Q意字W。例如,'[a-z]' 可以匚w 'a' ?'z' 范围内的L写字母字符?/td> </tr> <tr> <td style="FONT-SIZE: 12px">[^a-z] </td> <td style="FONT-SIZE: 12px">负值字W范围。匹配Q何不在指定范围内的Q意字W。例如,'[^a-z]' 可以匚wM不在 'a' ?'z' 范围内的L字符?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\b </td> <td style="FONT-SIZE: 12px">匚w一个单词边界,也就是指单词和空格间的位|。例如, 'er\b' 可以匚w"never" 中的 'er'Q但不能匚w "verb" 中的 'er'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\B </td> <td style="FONT-SIZE: 12px">匚w非单词边界?er\B' 能匹?"verb" 中的 'er'Q但不能匚w "never" 中的 'er'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\cx </td> <td style="FONT-SIZE: 12px">匚w?x 指明的控制字W。例如, \cM 匚w一?Control-M 或回车符。x 的值必Mؓ A-Z ?a-z 之一。否则,?c 视ؓ一个原义的 'c' 字符?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\d </td> <td style="FONT-SIZE: 12px">匚w一个数字字W。等价于 [0-9]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\D </td> <td style="FONT-SIZE: 12px">匚w一个非数字字符。等价于 [^0-9]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\f </td> <td style="FONT-SIZE: 12px">匚w一个换늬。等价于 \x0c ?\cL?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\n </td> <td style="FONT-SIZE: 12px">匚w一个换行符。等价于 \x0a ?\cJ?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\r </td> <td style="FONT-SIZE: 12px">匚w一个回车符。等价于 \x0d ?\cM?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\s </td> <td style="FONT-SIZE: 12px">匚wMI白字符Q包括空根{制表符、换늬{等。等价于 [ \f\n\r\t\v]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\S </td> <td style="FONT-SIZE: 12px">匚wM非空白字W。等价于 [^ \f\n\r\t\v]?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\t </td> <td style="FONT-SIZE: 12px">匚w一个制表符。等价于 \x09 ?\cI?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\v </td> <td style="FONT-SIZE: 12px">匚w一个垂直制表符。等价于 \x0b ?\cK?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\w </td> <td style="FONT-SIZE: 12px">匚w包括下划U的M单词字符。等价于'[A-Za-z0-9_]'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\W </td> <td style="FONT-SIZE: 12px">匚wM非单词字W。等价于 '[^A-Za-z0-9_]'?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\xn </td> <td style="FONT-SIZE: 12px">匚w nQ其?n 为十六进制{义倹{十六进制{义值必Mؓ定的两个数字长。例如,'\x41' 匚w "A"?\x041' 则等价于 '\x04' & "1"。正则表辑ּ中可以?ASCII ~码?</td> </tr> <tr> <td style="FONT-SIZE: 12px">\num </td> <td style="FONT-SIZE: 12px">匚w numQ其?num 是一个正整数。对所获取的匹配的引用。例如,'(.)\1' 匚w两个q箋的相同字W?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\n </td> <td style="FONT-SIZE: 12px">标识一个八q制转义值或一个向后引用。如?\n 之前臛_ n 个获取的子表辑ּQ则 n 为向后引用。否则,如果 n 为八q制数字 (0-7)Q则 n Z个八q制转义倹{?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\nm </td> <td style="FONT-SIZE: 12px">标识一个八q制转义值或一个向后引用。如?\nm 之前臛_?nm 个获得子表达式,?nm 为向后引用。如?\nm 之前臛_?n 个获取,?n Z个后跟文?m 的向后引用。如果前面的条g都不满Q若 n ?m 均ؓ八进制数?(0-7)Q则 \nm 匹配八q制转义?nm?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\nml </td> <td style="FONT-SIZE: 12px">如果 n 为八q制数字 (0-3)Q且 m ?l 均ؓ八进制数?(0-7)Q则匚w八进制{义?nml?/td> </tr> <tr> <td style="FONT-SIZE: 12px">\un </td> <td style="FONT-SIZE: 12px">匚w nQ其?n 是一个用四个十六q制数字表示?Unicode 字符。例如, \u00A9 匚w版权W号 (?)?/td> </tr> </tbody> </table> <p> </p> <h2><a target=_blank rel=nofollow name=6._部分例子>6. 部分例子</a></h2> <table cellSpacing=1 cellPadding=3 border=0> <tbody> <tr> <th style="FONT-SIZE: 12px" align=left>正则表达?/th> <th style="FONT-SIZE: 12px" align=left>说明</th> </tr> <tr> <td style="FONT-SIZE: 12px">/\b([a-z]+) \1\b/gi</td> <td style="FONT-SIZE: 12px">一个单词连l出现的位置</td> </tr> <tr> <td style="FONT-SIZE: 12px">/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/ </td> <td style="FONT-SIZE: 12px">一个URL解析为协议、域、端口及相对路径</td> </tr> <tr> <td style="FONT-SIZE: 12px">/^(?:Chapter|Section) [1-9][0-9]{0,1}$/</td> <td style="FONT-SIZE: 12px">定位章节的位|?/td> </tr> <tr> <td style="FONT-SIZE: 12px">/[-a-z]/</td> <td style="FONT-SIZE: 12px">A至z?6个字母再加一?受?/td> </tr> <tr> <td style="FONT-SIZE: 12px">/ter\b/</td> <td style="FONT-SIZE: 12px">可匹配chapterQ而不能terminal</td> </tr> <tr> <td style="FONT-SIZE: 12px">/\Bapt/</td> <td style="FONT-SIZE: 12px">可匹配chapterQ而不能aptitude</td> </tr> <tr> <td style="FONT-SIZE: 12px">/Windows(?=95 |98 |NT )/</td> <td style="FONT-SIZE: 12px">可匹配Windows95或Windows98或WindowsNT,当找C个匹配后Q从Windows后面开始进行下一ơ的索匹配?/td> </tr> </tbody> </table> <p> </p> <h2><a target=_blank rel=nofollow name=7._正则表达式匹配规?7. 正则表达式匹配规?/a></h2> <p><strong><a target=_blank rel=nofollow name=7.1_基本模式匚w>7.1 基本模式匚w</a></strong><br><br>  一切从最基本的开始。模式,是正规表辑ּ最基本的元素,它们是一l描q字W串特征的字W。模式可以很单,由普通的字符串组成,也可以非常复杂,往往用特D的字符表示一个范围内的字W、重复出玎ͼ或表CZ下文。例如:</p> <blockquote> <p>^once </p> </blockquote> <p>  q个模式包含一个特D的字符^Q表C模式只匹配那些以once开头的字符丌Ӏ例如该模式与字W串"once upon a time"匚wQ与"There once was a man from NewYork"不匹配。正如如^W号表示开头一P$W号用来匚w那些以给定模式结字符丌Ӏ?/p> <blockquote> <p>bucket$ </p> </blockquote> <p>  q个模式?Who kept all of this cash in a bucket"匚wQ与"buckets"不匹配。字W^?同时使用Ӟ表示_匚wQ字W串与模式一P。例如:</p> <blockquote> <p>^bucket$ </p> </blockquote> <p>  只匹配字W串"bucket"。如果一个模式不包括^?Q那么它与Q何包含该模式的字W串匚w。例如:模式</p> <blockquote> <p>once </p> </blockquote> <p>与字W串</p> <blockquote> <p>There once was a man from NewYork<br>Who kept all of his cash in a bucket.</p> </blockquote> <p>是匹配的?br><br>  在该模式中的字母(o-n-c-e)是字面的字符Q也是_他们表示该字母本w,数字也是一L。其他一些稍微复杂的字符Q如标点W号和白字符Q空根{制表符{)Q要用到转义序列。所有的转义序列都用反斜?\)打头。制表符的{义序列是Q\t。所以如果我们要一个字W串是否以制表符开_可以用这个模式:</p> <blockquote> <p>^\t </p> </blockquote> <p>cM的,用\n表示“新行”Q\r表示回R。其他的ҎW号Q可以用在前面加上反斜杠Q如反斜杠本w用\\表示Q句?用\.表示Q以此类推?br><br><strong><a target=_blank rel=nofollow name=7.2_字符?7.2 字符?/a></strong><br><br>在INTERNET的程序中Q正规表辑ּ通常用来验证用户的输入。当用户提交一个form以后Q要判断输入的电话号码、地址、EMAIL地址、信用卡L{是否有效,用普通的Z字面的字W是不够的?br><br>所以要用一U更自由的描q我们要的模式的办法Q它是字符。要建立一个表C所有元韛_W的字符,把所有的元音字符攑֜一个方括号里:</p> <blockquote> <p>[AaEeIiOoUu] </p> </blockquote> <p>q个模式与Q何元韛_W匹配,但只能表CZ个字W。用q字号可以表CZ个字W的范围Q如Q?/p> <blockquote> <p>[a-z] //匚w所有的写字母 <br>[A-Z] //匚w所有的大写字母 <br>[a-zA-Z] //匚w所有的字母 <br>[0-9] //匚w所有的数字 <br>[0-9\.\-] //匚w所有的数字Q句号和减号 <br>[ \f\r\t\n] //匚w所有的白字W?</p> </blockquote> <p>同样的,q些也只表示一个字W,q是一个非帔R要的。如果要匚w一个由一个小写字母和一位数字组成的字符Ԍ比如"z2"?t6"?g7"Q但不是"ab2"?r2d3" ?b52"的话Q用q个模式Q?/p> <blockquote> <p>^[a-z][0-9]$ </p> </blockquote> <p>管[a-z]代表26个字母的范围Q但在这里它只能与第一个字W是写字母的字W串匚w?br><br>前面曄提到^表示字符串的开_但它q有另外一个含义。当在一l方括号里用^是,它表C?#8220;?#8221;?#8220;排除”的意思,常常用来剔除某个字符。还用前面的例子Q我们要求第一个字W不能是数字Q?/p> <blockquote> <p>^[^0-9][0-9]$ </p> </blockquote> <p>q个模式?&5"?g7"?-2"是匹配的Q但?12"?66"是不匚w的。下面是几个排除特定字符的例子:</p> <blockquote> <p>[^a-z] //除了写字母以外的所有字W?<br>[^\\\/\^] //除了(\)(/)(^)之外的所有字W?<br>[^\"\'] //除了双引?")和单引号(')之外的所有字W?</p> </blockquote> <p>Ҏ字符"." (点,句号)在正规表辑ּ中用来表C除?#8220;新行”之外的所有字W。所以模?^.5$"与Q何两个字W的、以数字5l尾和以其他?#8220;新行”字符开头的字符串匹配。模?."可以匚wM字符Ԍ除了IZ和只包括一?#8220;新行”的字W串?br><br>PHP的正规表辑ּ有一些内|的通用字符,列表如下Q?/p> <blockquote> <p>字符?含义 <br>[[:alpha:]] M字母 <br>[[:digit:]] M数字 <br>[[:alnum:]] M字母和数?<br>[[:space:]] M白字W?<br>[[:upper:]] M大写字母 <br>[[:lower:]] M写字母 <br>[[:punct:]] M标点W号 <br>[[:xdigit:]] M16q制的数字,相当于[0-9a-fA-F] </p> </blockquote> <p><strong><a target=_blank rel=nofollow name=7.3_定重复出现>7.3 定重复出现</a></strong><br><br>到现在ؓ止,你已l知道如何去匚w一个字母或数字Q但更多的情况下Q可能要匚w一个单词或一l数字。一个单词有若干个字母组成,一l数字有若干个单数组成。跟在字W或字符后面的花括?{})用来定前面的内容的重复出现的次数?</p> <blockquote> <p>字符?含义 <br>^[a-zA-Z_]$ 所有的字母和下划线 <br>^[[:alpha:]]{3}$ 所有的3个字母的单词 <br>^a$ 字母a <br>^a{4}$ aaaa <br>^a{2,4}$ aa,aaa或aaaa <br>^a{1,3}$ a,aa或aaa <br>^a{2,}$ 包含多于两个a的字W串 <br>^a{2,} 如:aardvark和aaabQ但apple不行 <br>a{2,} 如:baad和aaaQ但Nantucket不行 <br>\t{2} 两个制表W?<br>.{2} 所有的两个字符 </p> </blockquote> <p>q些例子描述了花括号的三U不同的用法。一个数字,{x}的意思是“前面的字W或字符只出现x?#8221;Q一个数字加逗号Q{x,}的意思是“前面的内容出现x或更多的ơ数”Q两个用逗号分隔的数字,{x,y}表示“前面的内容至出现xơ,但不过y?#8221;。我们可以把模式扩展到更多的单词或数字:</p> <blockquote> <p>^[a-zA-Z0-9_]{1,}$ //所有包含一个以上的字母、数字或下划U的字符?<br>^[0-9]{1,}$ //所有的正数 <br>^\-{0,1}[0-9]{1,}$ //所有的整数 <br>^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$ //所有的数 </p> </blockquote> <p>最后一个例子不太好理解Q是吗?q么看吧Q与所有以一个可选的负号(\-{0,1})开?^)、跟着0个或更多的数?[0-9]{0,})、和一个可选的数?\.{0,1})再跟?个或多个数字([0-9]{0,})Qƈ且没有其他Q何东?$)。下面你知道能够用的更ؓ单的Ҏ?br><br>Ҏ字符"?"与{0,1}是相{的Q它们都代表着Q?#8220;0个或1个前面的内容”?#8220;前面的内Ҏ可选的”。所以刚才的例子可以化ؓQ?/p> <blockquote> <p>^\-?[0-9]{0,}\.?[0-9]{0,}$ </p> </blockquote> <p>Ҏ字符"*"与{0,}是相{的Q它们都代表着“0个或多个前面的内?#8221;。最后,字符"+"?{1,}是相{的Q表C?#8220;1个或多个前面的内?#8221;Q所以上面的4个例子可以写成:</p> <blockquote> <p>^[a-zA-Z0-9_]+$ //所有包含一个以上的字母、数字或下划U的字符?<br>^[0-9]+$ //所有的正数 <br>^\-?[0-9]+$ //所有的整数 <br>^\-?[0-9]*\.?[0-9]*$ //所有的数 </p> </blockquote> <p>当然qƈ不能从技术上降低正规表达式的复杂性,但可以它们更容易阅诅R?/p> <img src ="http://www.tkk7.com/masen/aggbug/123365.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/masen/" target="_blank">Masen</a> 2007-06-11 12:59 <a href="http://www.tkk7.com/masen/articles/123365.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文g路径的处?转帖) http://www.tkk7.com/masen/articles/108656.htmlMasenMasenThu, 05 Apr 2007 03:47:00 GMThttp://www.tkk7.com/masen/articles/108656.htmlhttp://www.tkk7.com/masen/comments/108656.htmlhttp://www.tkk7.com/masen/articles/108656.html#Feedback0http://www.tkk7.com/masen/comments/commentRss/108656.htmlhttp://www.tkk7.com/masen/services/trackbacks/108656.html1.如何获得当前文g路径

常用Q?/font>

字符串类型:System.getProperty("user.dir");

l合Q?/font>

package com.zcjl.test.base;
import java.io.File;
public class Test {
    public static void main(String[] args) throws Exception {
        System.out.println(
            Thread.currentThread().getContextClassLoader().getResource(""));
        System.out.println(Test.class.getClassLoader().getResource(""));
        System.out.println(ClassLoader.getSystemResource(""));
        System.out.println(Test.class.getResource(""));
        System.out.println(Test.class.getResource("/"));
        System.out.println(new File("").getAbsolutePath());
        System.out.println(System.getProperty("user.dir"));

    }
}

2.Web服务?/font>

(1).Weblogic

WebApplication的系l文件根目录是你的weblogic安装所在根目录?br>例如Q如果你的weblogic安装在c:\bea\weblogic700.....
那么Q你的文件根路径是c:\.
所以,有两U方式能够让你访问你的服务器端的文gQ?br>a.使用l对路径Q?br>比如你的参数文件放在c:\yourconfig\yourconf.propertiesQ?br>直接使用 new FileInputStream("yourconfig/yourconf.properties");
b.使用相对路径Q?br>相对路径的根目录是你的webapplication的根路径Q即WEB-INF的上一U目录,你的参数文件放在yourwebapp\yourconfig\yourconf.propertiesQ?br>q样使用Q?br>new FileInputStream("./yourconfig/yourconf.properties");
q两U方式均可,自己选择?/font>

(2).Tomcat

在类中输出System.getProperty("user.dir");昄的是%Tomcat_Home%/bin

(3).Resin

不是你的JSP攄相对路径,是JSP引擎执行q个JSP~译成SERVLET
的\径ؓ?比如用新建文件法试File f = new File("a.htm");
q个a.htm在resin的安装目录下

(4).如何ȝ对\径哪Q?/font>

在Java文g中getResource或getResourceAsStream均可

例:getClass().getResourceAsStream(filePath);//filePath可以?/filename",q里?代表web发布根\径下WEB-INF/classes

(5).获得文g真实路径

string  file_real_path=request.getRealPath("mypath/filename"); 

通常使用request.getRealPath("/"); 

3.文g操作的类

import java.io.*;
import java.net.*;
import java.util.*;
//import javax.swing.filechooser.*;
//import org.jr.swing.filter.*;

/**
* 此类中封装一些常用的文g操作?br>* 所有方法都是静态方法,不需要生成此cȝ实例Q?br>* 为避免生成此cȝ实例Q构造方法被x为privatecd的?br>* @since  0.1
*/

public class FileUtil {
  /**
   * U有构造方法,防止cȝ实例化,因ؓ工具cM需要实例化?br>   */
  private FileUtil() {

  }

  /**
   * 修改文g的最后访问时间?br>   * 如果文g不存在则创徏该文件?br>   * <b>目前q个Ҏ的行为方式还不稳定,主要是方法有些信息输出,q些信息输出是否保留q在?/font>

虑中?lt;/b>
   * @param file 需要修Ҏ后访问时间的文g?br>   * @since  0.1
   */
  public static void touch(File file) {
    long currentTime = System.currentTimeMillis();
    if (!file.exists()) {
      System.err.println("file not found:" + file.getName());
      System.err.println("Create a new file:" + file.getName());
      try {
        if (file.createNewFile()) {
        //  System.out.println("Succeeded!");
        }
        else {
        //  System.err.println("Create file failed!");
        }
      }
      catch (IOException e) {
      //  System.err.println("Create file failed!");
        e.printStackTrace();
      }
    }
    boolean result = file.setLastModified(currentTime);
    if (!result) {
    //  System.err.println("touch failed: " + file.getName());
    }
  }

  /**
   * 修改文g的最后访问时间?br>   * 如果文g不存在则创徏该文件?br>   * <b>目前q个Ҏ的行为方式还不稳定,主要是方法有些信息输出,q些信息输出是否保留q在?/font>

虑中?lt;/b>
   * @param fileName 需要修Ҏ后访问时间的文g的文件名?br>   * @since  0.1
   */
  public static void touch(String fileName) {
    File file = new File(fileName);
    touch(file);
  }

  /**
   * 修改文g的最后访问时间?br>   * 如果文g不存在则创徏该文件?br>   * <b>目前q个Ҏ的行为方式还不稳定,主要是方法有些信息输出,q些信息输出是否保留q在?/font>

虑中?lt;/b>
   * @param files 需要修Ҏ后访问时间的文g数组?br>   * @since  0.1
   */
  public static void touch(File[] files) {
    for (int i = 0; i < files.length; i++) {
      touch(files);
    }
  }

  /**
   * 修改文g的最后访问时间?br>   * 如果文g不存在则创徏该文件?br>   * <b>目前q个Ҏ的行为方式还不稳定,主要是方法有些信息输出,q些信息输出是否保留q在?/font>

虑中?lt;/b>
   * @param fileNames 需要修Ҏ后访问时间的文g名数l?br>   * @since  0.1
   */
  public static void touch(String[] fileNames) {
    File[] files = new File[fileNames.length];
    for (int i = 0; i < fileNames.length; i++) {
      files = new File(fileNames);
    }
    touch(files);
  }

  /**
   * 判断指定的文件是否存在?br>   * @param fileName 要判断的文g的文件名
   * @return 存在时返回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean isFileExist(String fileName) {
    return new File(fileName).isFile();
  }

  /**
   * 创徏指定的目录?br>   * 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录?br>   * <b>注意Q可能会在返回false的时候创建部分父目录?lt;/b>
   * @param file 要创建的目录
   * @return 完全创徏成功时返回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean makeDirectory(File file) {
    File parent = file.getParentFile();
    if (parent != null) {
      return parent.mkdirs();
    }
    return false;
  }

  /**
   * 创徏指定的目录?br>   * 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录?br>   * <b>注意Q可能会在返回false的时候创建部分父目录?lt;/b>
   * @param fileName 要创建的目录的目录名
   * @return 完全创徏成功时返回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean makeDirectory(String fileName) {
    File file = new File(fileName);
    return makeDirectory(file);
  }

  /**
   * 清空指定目录中的文g?br>   * q个Ҏ尽可能删除所有的文gQ但是只要有一个文件没有被删除都会q回false?br>   * 另外q个Ҏ不会q代删除Q即不会删除子目录及其内宏V?br>   * @param directory 要清I的目录
   * @return 目录下的所有文仉被成功删除时q回trueQ否则返回false.
   * @since  0.1
   */
  public static boolean emptyDirectory(File directory) {
    boolean result = false;
    File[] entries = directory.listFiles();
    for (int i = 0; i < entries.length; i++) {
      if (!entries.delete()) {
        result = false;
      }
    }
    return true;
  }

  /**
   * 清空指定目录中的文g?br>   * q个Ҏ尽可能删除所有的文gQ但是只要有一个文件没有被删除都会q回false?br>   * 另外q个Ҏ不会q代删除Q即不会删除子目录及其内宏V?br>   * @param directoryName 要清I的目录的目录名
   * @return 目录下的所有文仉被成功删除时q回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean emptyDirectory(String directoryName) {
    File dir = new File(directoryName);
    return emptyDirectory(dir);
  }

  /**
   * 删除指定目录及其中的所有内宏V?br>   * @param dirName 要删除的目录的目录名
   * @return 删除成功时返回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean deleteDirectory(String dirName) {
    return deleteDirectory(new File(dirName));
  }

  /**
   * 删除指定目录及其中的所有内宏V?br>   * @param dir 要删除的目录
   * @return 删除成功时返回trueQ否则返回false?br>   * @since  0.1
   */
  public static boolean deleteDirectory(File dir) {
    if ( (dir == null) || !dir.isDirectory()) {
      throw new IllegalArgumentException("Argument " + dir +
                                         " is not a directory. ");
    }

    File[] entries = dir.listFiles();
    int sz = entries.length;

    for (int i = 0; i < sz; i++) {
      if (entries.isDirectory()) {
        if (!deleteDirectory(entries)) {
          return false;
        }
      }
      else {
        if (!entries.delete()) {
          return false;
        }
      }
    }

    if (!dir.delete()) {
      return false;
    }
    return true;
  }


  /**
   * q回文g的URL地址?br>   * @param file 文g
   * @return 文g对应的的URL地址
   * @throws MalformedURLException
   * @since  0.4
   * @deprecated 在实现的时候没有注意到FilecLw带一个toURLҎ文件\径{换ؓURL?br>   *             请用File.toURLҎ?br>   */
  public static URL getURL(File file) throws MalformedURLException {
    String fileURL = "file:/" + file.getAbsolutePath();
    URL url = new URL(fileURL);
    return url;
  }

  /**
   * 从文件\径得到文件名?br>   * @param filePath 文g的\径,可以是相对\径也可以是绝对\?br>   * @return 对应的文件名
   * @since  0.4
   */
  public static String getFileName(String filePath) {
    File file = new File(filePath);
    return file.getName();
  }

  /**
   * 从文件名得到文gl对路径?br>   * @param fileName 文g?br>   * @return 对应的文件\?br>   * @since  0.4
   */
  public static String getFilePath(String fileName) {
    File file = new File(fileName);
    return file.getAbsolutePath();
  }

  /**
   * DOS/Windows格式的\径{换ؓUNIX/Linux格式的\径?br>   * 其实是\径中?\"全部换ؓ"/"Q因为在某些情况下我们{换ؓq种方式比较方便Q?br>   * 某中E度上说"/"?\"更适合作ؓ路径分隔W,而且DOS/Windows也将它当作\径分隔符?br>   * @param filePath 转换前的路径
   * @return 转换后的路径
   * @since  0.4
   */
  public static String toUNIXpath(String filePath) {
    return filePath.replace('\\', '/');
  }

  /**
   * 从文件名得到UNIX风格的文件绝对\径?br>   * @param fileName 文g?br>   * @return 对应的UNIX风格的文件\?br>   * @since  0.4
   * @see #toUNIXpath(String filePath) toUNIXpath
   */
  public static String getUNIXfilePath(String fileName) {
    File file = new File(fileName);
    return toUNIXpath(file.getAbsolutePath());
  }

  /**
   * 得到文g的类型?br>   * 实际上就是得到文件名中最后一?#8220;.”后面的部分?br>   * @param fileName 文g?br>   * @return 文g名中的类型部?br>   * @since  0.5
   */
  public static String getTypePart(String fileName) {
    int point = fileName.lastIndexOf('.');
    int length = fileName.length();
    if (point == -1 || point == length - 1) {
      return "";
    }
    else {
      return fileName.substring(point + 1, length);
    }
  }

  /**
   * 得到文g的类型?br>   * 实际上就是得到文件名中最后一?#8220;.”后面的部分?br>   * @param file 文g
   * @return 文g名中的类型部?br>   * @since  0.5
   */
  public static String getFileType(File file) {
    return getTypePart(file.getName());
  }

  /**
   * 得到文g的名字部分?br>   * 实际上就是\径中的最后一个\径分隔符后的部分?br>   * @param fileName 文g?br>   * @return 文g名中的名字部?br>   * @since  0.5
   */
  public static String getNamePart(String fileName) {
    int point = getPathLsatIndex(fileName);
    int length = fileName.length();
    if (point == -1) {
      return fileName;
    }
    else if (point == length - 1) {
      int secondPoint = getPathLsatIndex(fileName, point - 1);
      if (secondPoint == -1) {
        if (length == 1) {
          return fileName;
        }
        else {
          return fileName.substring(0, point);
        }
      }
      else {
        return fileName.substring(secondPoint + 1, point);
      }
    }
    else {
      return fileName.substring(point + 1);
    }
  }

  /**
   * 得到文g名中的父路径部分?br>   * 对两U\径分隔符都有效?br>   * 不存在时q回""?br>   * 如果文g名是以\径分隔符l尾的则不考虑该分隔符Q例?/path/"q回""?br>   * @param fileName 文g?br>   * @return 父\径,不存在或者已l是父目录时q回""
   * @since  0.5
   */
  public static String getPathPart(String fileName) {
    int point = getPathLsatIndex(fileName);
    int length = fileName.length();
    if (point == -1) {
      return "";
    }
    else if (point == length - 1) {
      int secondPoint = getPathLsatIndex(fileName, point - 1);
      if (secondPoint == -1) {
        return "";
      }
      else {
        return fileName.substring(0, secondPoint);
      }
    }
    else {
      return fileName.substring(0, point);
    }
  }

  /**
   * 得到路径分隔W在文g路径中首ơ出现的位置?br>   * 对于DOS或者UNIX风格的分隔符都可以?br>   * @param fileName 文g路径
   * @return 路径分隔W在路径中首ơ出现的位置Q没有出现时q回-1?br>   * @since  0.5
   */
  public static int getPathIndex(String fileName) {
    int point = fileName.indexOf('/');
    if (point == -1) {
      point = fileName.indexOf('\\');
    }
    return point;
  }

  /**
   * 得到路径分隔W在文g路径中指定位|后首次出现的位|?br>   * 对于DOS或者UNIX风格的分隔符都可以?br>   * @param fileName 文g路径
   * @param fromIndex 开始查扄位置
   * @return 路径分隔W在路径中指定位|后首次出现的位|,没有出现时返?1?br>   * @since  0.5
   */
  public static int getPathIndex(String fileName, int fromIndex) {
    int point = fileName.indexOf('/', fromIndex);
    if (point == -1) {
      point = fileName.indexOf('\\', fromIndex);
    }
    return point;
  }

  /**
   * 得到路径分隔W在文g路径中最后出现的位置?br>   * 对于DOS或者UNIX风格的分隔符都可以?br>   * @param fileName 文g路径
   * @return 路径分隔W在路径中最后出现的位置Q没有出现时q回-1?br>   * @since  0.5
   */
  public static int getPathLsatIndex(String fileName) {
    int point = fileName.lastIndexOf('/');
    if (point == -1) {
      point = fileName.lastIndexOf('\\');
    }
    return point;
  }

  /**
   * 得到路径分隔W在文g路径中指定位|前最后出现的位置?br>   * 对于DOS或者UNIX风格的分隔符都可以?br>   * @param fileName 文g路径
   * @param fromIndex 开始查扄位置
   * @return 路径分隔W在路径中指定位|前最后出现的位置Q没有出现时q回-1?br>   * @since  0.5
   */
  public static int getPathLsatIndex(String fileName, int fromIndex) {
    int point = fileName.lastIndexOf('/', fromIndex);
    if (point == -1) {
      point = fileName.lastIndexOf('\\', fromIndex);
    }
    return point;
  }

  /**
   * 文件名中的cd部分L?br>   * @param filename 文g?br>   * @return Lcd部分的结?br>   * @since  0.5
   */
  public static String trimType(String filename) {
    int index = filename.lastIndexOf(".");
    if (index != -1) {
      return filename.substring(0, index);
    }
    else {
      return filename;
    }
  }
  /**
   * 得到相对路径?br>   * 文g名不是目录名的子节点时返回文件名?br>   * @param pathName 目录?br>   * @param fileName 文g?br>   * @return 得到文g名相对于目录名的相对路径Q目录下不存在该文g时返回文件名
   * @since  0.5
   */
  public static String getSubpath(String pathName,String fileName) {
    int index = fileName.indexOf(pathName);
    if (index != -1) {
      return fileName.substring(index + pathName.length() + 1);
    }
    else {
      return fileName;
    }
  }

}
 4.遗留问题

目前new FileInputStream()只会使用l对路径Q相Ҏ用过Q因相对于web服务器地址Q比较麻?/font>

q不如写个配|文件来的快?/font>

5.按Java文gcd分类d配置文g

配置文g是应用系l中不可~少的,可以增加E序的灵zL。java.util.Properties是从jdk1.2有的类Q一直到现在都支持load ()ҎQjdk1.4以后save(output,string) ->store(output,string)。如果只是单U的读,Ҏ不存在烦恼的问题。web层可以通过 Thread.currentThread().getContextClassLoader().
getResourceAsStream("xx.properties") 获取QApplication可以通过new FileInputStream("xx.properties");直接在classes一U获取。关键是有时我们需要通过web修改配置文gQ我们不能将路径写死了。经q测试觉得有以下心得Q?/font>

1.servlet中读写。如果运用Struts 或者Servlet可以直接在初始化参数中配|,调用时根据servlet的getRealPath("/")获取真实路径Q再ҎString file = this.servlet.getInitParameter("abc");获取相对的WEB-INF的相对\径?br>例:
InputStream input = Thread.currentThread().getContextClassLoader().
getResourceAsStream("abc.properties");
Properties prop = new Properties();
prop.load(input);
input.close();
OutputStream out = new FileOutputStream(path);
prop.setProperty("abc", “test");
prop.store(out, “–test–");
out.close();

2.直接在jsp中操作,通过jsp内置对象获取可操作的l对地址?br>例:
// jsp面
String path = pageContext.getServletContext().getRealPath("/");
String realPath = path+"/WEB-INF/classes/abc.properties";

//java E序
InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties攑֜webroot/WEB-INF/classes/目录?br>prop.load(in);
in.close();

OutputStream out = new FileOutputStream(path); // path为通过面传入的\?br>prop.setProperty("abc", “abcccccc");
prop.store(out, “–test–");
out.close();

3.只通过JavaE序操作资源文g
InputStream in = new FileInputStream("abc.properties"); // 攑֜classes同

OutputStream out = new FileOutputStream("abc.properties");



Masen 2007-04-05 11:47 发表评论
]]>
Struts多模块开?/title><link>http://www.tkk7.com/masen/articles/106277.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Sun, 25 Mar 2007 14:20:00 GMT</pubDate><guid>http://www.tkk7.com/masen/articles/106277.html</guid><wfw:comment>http://www.tkk7.com/masen/comments/106277.html</wfw:comment><comments>http://www.tkk7.com/masen/articles/106277.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.tkk7.com/masen/comments/commentRss/106277.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/masen/services/trackbacks/106277.html</trackback:ping><description><![CDATA[ <p>在基于struts的WEB应用的开发中,使用多模块开发是十分必要?ȝ了一下如?</p> <p>Struts 的配|文件struts-config.xml 是Struts 框架最重要的资源之一Qƈ且是需要频J改动的。如果ƈ行开发的各个团队都是用这一个配|文Ӟ势必造成讉K冲突。Strus 框架的模块化机制是专门应对q种情况的?/p> <p>Struts ?.1 版本开始增加了模块化支持,q且一直在强化Ҏ块化的支持。不同的应用模块可以拥有各自的struts-config 配置文g、消息资源、Validator 框架配置文g。不同的模块可以协同开发,互不影响?/p> <p>Struts 应用配置为多模块应用Q需要如下三个步骤:</p> <p>为每个模块分别徏立一个struts配置文gQ?<br />通知模块控制器; <br />使用特定的Action在模块间跌{?<br />一 l各模块命名:</p> <p>struts默认的配|文件名是struts-config.xml,l其它模块配|文件命名struts-config-***.xml.比如d模块需要一个单独的模块可以命名为struts-config-login.xml;</p> <p>?通知模块控制?</p> <p>在web.xml中加入如下代?</p> <p><init-param>     <br />      <param-name>config</param-name>     <br />      <param-value>/WEB-INF/conf/struts-config.xml</param-value>     <br /></init-param>     <br /><init-param>     <br />      <param-name>config/login</param-name>     <br />      <param-value>/WEB-INF/conf/login/struts-config-login.xml</param-value>     <br /></init-param>    </p> <p>配置文g对应的ActionServlet 初始化参数名为config。对于其他模块,ActionServlet 初始化参数的命名原则是“config/模块名”。如上面的代码示例中Qlogin 模块的配|文件对应的初始化参Cؓconfig/login。其中前~“config/”是不能~少的,后面跟模块名。在Struts 控制器中Q是通过模块名来区分不同模块的。在资源讉K中,也是一模块名作为前~来区分对不同模块的访问。如以?login”开头的路径会告诉控制器所要访问的是login 模块的资源?/p> <p>?实现模块间蟩?</p> <p>(1)用SwitchAction,主要用于实现面的蟩?/p> <p>(2)全局转发和局部{?用于实现模块间的跌{</p> <p>(3)采用html:link标签</p> <p>多模块开发示例:QEclipse3.2+MyEclipse5.0Q?<br />1Q新Z个Web Project <br />2Q添加Struts 1.2 支持 <br />3Q在根目录(WebRootQ下新徏index.jsp面 <br />4Q在WebRoot目录下新Z个模块目录“ModuleA”?<br />5Q新Z个struts 1.2 moduleQ名为“ModuleA”?<br />6Q测试直接链接到其他模块的jsp面Q注意不能直接链接到其他模块的与Action、ActionForm相关联的面Q:</p> <p>在根目录的index.jsp面中添加如下代码:</p> <p><html:link module="/ModuleA" page="/index.jsp">1.链接到其他模块(ModuleAQ直接链接到面</html:link><br>适用于没有和ActionQActionForm兌的纯JSP面<br><p></p> <p>在ModuleA目录下新建index.jsp面Q完成后试成功?<br /><br />7Q测试直接链接到其他模块的jsp面Q直接链接到其他模块的与Action、ActionForm相关联的面Q应该不能成功)Q?<br />首先配置struts-config-ModuleA.xml文gQ?<br />新徏一个Form、Action and JSPQ用例中填写userQ添加两个属性:nameQpassword?<br />在JSP选项卡中填写?ModuleA/login.jsp”,点击下一步,在Path中填写?login”,在forward选项卡中d一个forwardQname为“OK”,Path为?welcome.jsp”,点击完成?<br />在ModuleA目录下新建wellcome.jsp文gQ在源码中加入如下代码: <br /><bean:write name=”userForm?property=”name?/>.在UserAction.java源码中填写如下代?return mapping.findForward(“OK??<br />在根目录下的index.jsp源码中添加如下代码: <br /><html:link module="/ModuleA" page="/login.jsp">2.链接到其他模块(ModuleAQ试图连接到和ActionQActionForm兌的页?lt;/html:link><br>不能直接q样链接Q会报Cannot retrieve mapping for action /xxx异常Q因为此时还是检查struts-config.xml<br><p> <br />试后果然异常?<br />正确做法如下Q?<br />在struts-config-ModuleA.xml文g中添加一个ActionQ在用例中填写“toLoginPage”,在forward选项卡中d一个forwardQname为“toLoginPage”,Path为?login.jsp”?<br />修改ToLoginPage.java的源码ؓQreturn mapping.findForward(“toLoginPage?;?<br />在根目录下的index.jsp源码中添加如下代码: <br /><html:link module="/ModuleA" page="/toLoginPage.do">3.链接到其他模块(ModuleAQ的xxxx.doQ连接到和ActionQActionForm兌的页?lt;/html:link><br>正确做法Q是?链接形式的更正,应该链接到ModuleA的一个ActionQ由该Action的forward转发到login.jsp面<br><p> <br />l测试成功?/p> <p>8Q测试跨模块表单提交Q从L块提交表单到ModuleA模块Q: <br />在struts-config.xmlQ主模块Q中新徏Form、Action and JSPQ用例中输入“reg”,增加两个属性:sex、ageQ点M一步,在Path中填写?userReg”,Action Impl中选择Use existing Action classQ用SwitchActionQ直接finish掉。添加根目录下的indexs.jsp文g源码如下Q?/p> <p>4.提交表单到其他模块:<br> <br /><html:form action="/userReg?prefix=/ModuleA&page=/regOK.jsp"> <br />sex : <html:text property="sex"/><html:errors property="sex"/><br/> <br />age : <html:text property="age"/><html:errors property="age"/><br/> <br /><html:submit/><html:cancel/> <br /></html:form><br><p>?/p> <p>在ModuleA文g夹下新徏regOK.jsp文gQ在regOK.jsp文g源码中写入如下语句: <br />regOK,your sex is:<bean:write name="regForm" property="sex"/> age is:<bean:write name="regForm" property="age"/> <br />l测试成功?/p> <p>9Q测试跨模块表单提交Q从ModuleA模块提交表单到ModuleB模块Q: <br />新徏struts1.2模块Q叫“ModuleB”,在WebRoot目录下新建文件夹“ModuleB”,配置struts-config-ModuleB.xml文gQ?<br />新徏Form、Action and JSPQ在用例中输入“employee”,增加两个属性:empName、depNameQ在JSP选项卡中填写?ModuleA/empInfo.jsp”,点击下一步,Path中填写?employee”,在Action Impl中选择Use existing Action classQ用SwitchActionQ直接finish掉?<br />修改empInfo.jsp文g源码Q?lt;html:form action="/employee?prefix=/ModuleB&page=/showEmp.jsp">…?<br />在ModuleB文g夹中新徏showEmp.jsp文gQ修改showemp.jsp源码为:<bean:write name="employeeForm" property="empName"/>?<br />在struts-config-ModuleA.xml文g中,增加一个ActionQ在用例中输入“empInfo”,增加一个forwardQname为?empInfo.jsp”。在根目录下的index.jsp文g中增加如下内容: <br />5.提交表单到其他模块(子模块到子模块)Q?lt;br> <br /><html:link module="/ModuleA" page="/empInfo.do">链接到模块(ModuleBQ的empInfo.do</html:link><br>链接到ModuleA的一个ActionQ由该Action的forward转发到empInfo.jsp面<br><p> <br />最后修改EmpInfoAction.java源码Qreturn mapping.findForward("empOK"); <br />l测试成功?/p> <img src ="http://www.tkk7.com/masen/aggbug/106277.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/masen/" target="_blank">Masen</a> 2007-03-25 22:20 <a href="http://www.tkk7.com/masen/articles/106277.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts中的3UActionc?/title><link>http://www.tkk7.com/masen/articles/105861.html</link><dc:creator>Masen</dc:creator><author>Masen</author><pubDate>Fri, 23 Mar 2007 07:58:00 GMT</pubDate><guid>http://www.tkk7.com/masen/articles/105861.html</guid><wfw:comment>http://www.tkk7.com/masen/comments/105861.html</wfw:comment><comments>http://www.tkk7.com/masen/articles/105861.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/masen/comments/commentRss/105861.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/masen/services/trackbacks/105861.html</trackback:ping><description><![CDATA[ <p>DispatchAction, LookupDispatchAction, MappingDispatchAction</p> <p> <br />1) DispatchAction是在struts-config中用parameter参数配置一个表单字D名,q个字段的值就是最l替代execute被调用的Ҏ. 例如parameter="method"而request.getParameter("method")="save"Q其?save"是MethodName。struts的请求将Ҏparameter被分发到"save"或?edit"或者什么。但是有一点,save()或者edit(){方法的声明和execute必须一模一栗同Ӟ删除以前的execute(----)ҎQ如:<br />-------<br />-------<br />public class UserAction extends DispatchAction </p> <p>{ </p> <p>    public ActionForward addUser (ActionMapping mapping,ActionForm form, </p> <p>            HttpServletRequest request,HttpServletResponse response) throws Exception </p> <p>    { </p> <p>             // 增加用户业务的逻辑 </p> <p>            return mapping.findForward(Constant. FORWARD_ADD ); </p> <p>    } </p> <p>    </p> <p>    public ActionForward delUser(ActionMapping mapping,ActionForm form, </p> <p>            HttpServletRequest request,HttpServletResponse response) throws Exception </p> <p>    { </p> <p>             // 删除用户业务的逻辑 </p> <p>            return mapping.findForward(Constant. FORWARD_SUCCESS ); </p> <p>    } </p> <p> </p> <p>    public ActionForward updateUser(ActionMapping mapping,ActionForm form, </p> <p>            HttpServletRequest request,HttpServletResponse response) throws Exception </p> <p>    { </p> <p>             // 更新用户业务的逻辑 </p> <p>            return mapping.findForward(Constant. FORWARD_SUCCESS ); </p> <p>    } </p> <p>} </p> <p> </p> <p>2) LookupDispatchActionl承DispatchAction, 用于对同一个页面上的多个submit按钮q行不同的响应。其原理是,首先用MessageResource按钮的文本和ResKey相关联,例如button.save=saveQ然后再复写getKeyMethodMap(), ResKey和MethodName对应h, 例如map.put("button.save", "save"); 光|方法和DispatchAction是一L,  parameter属性ؓQmethod 如:<br /><html:submit property="method"><br /><bean:message key="button.save"/><br /></html:submit><br /><html:submit property="method"><br /><bean:message key="button.delete"/><br /></html:submit><br />注意Q此Action一定要有对应的FormBeanQ?是说一定要有name属性, 同时要删除此cȝexecute(-----)Ҏ<br />资源文g?<br />button.save=save<br />button.delete=delete</p> <p>3) MappingDispatchAction?.2新加? 也承自DispatchAction. 它实现的功能和上面两个区别较? 是通过struts-config.xml多个action-mapping映射到同一个Actioncȝ不同Ҏ? 典型的配|就?<br /><action-mappings><br /><action path="/saveUser" type="logic.UserAction" parameter="save"></action><br /><action path="/editUser" type="logic.UserAction" parameter="edit"></action><br /></action-mappings><br />然后UserActionl承MappingDispatchActionQ其中有Q?br />public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception <br />public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception <br />{方?br /></p> <img src ="http://www.tkk7.com/masen/aggbug/105861.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/masen/" target="_blank">Masen</a> 2007-03-23 15:58 <a href="http://www.tkk7.com/masen/articles/105861.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://01shanzhai.com" target="_blank">һƵѹۿ</a>| <a href="http://cz655.com" target="_blank">޵һƵ߹ۿ</a>| <a href="http://sds54.com" target="_blank">97ƵѹƵ</a>| <a href="http://xsxdsb.com" target="_blank">ɫ͵͵ۺAV </a>| <a href="http://3atv3523.com" target="_blank">С˵ͼƬ</a>| <a href="http://joob4s.com" target="_blank">ձ߸Ѱվ</a>| <a href="http://116com.com" target="_blank">ľƷ</a>| <a href="http://www621f.com" target="_blank">ŮɫëƬѿ</a>| <a href="http://7272004.com" target="_blank">ŷպĸwww777</a>| <a href="http://513573.com" target="_blank">޳߲</a>| <a href="http://33eaa.com" target="_blank">޸Ƶ</a>| <a href="http://118762.com" target="_blank">ҹ޾Ʒҹ³˿Ƭ</a>| <a href="http://wwwfac37.com" target="_blank">ĻӰѹۿַ</a>| <a href="http://1314c.com" target="_blank">ɫWWWƷƵ </a>| <a href="http://tbw77.com" target="_blank">޺һ</a>| <a href="http://0515zs.com" target="_blank">þþӰԺ޾Ʒ</a>| <a href="http://714747.com" target="_blank">AVרAV</a>| <a href="http://zc-zk.com" target="_blank">޹ۺ߾Ʒ </a>| <a href="http://daohang123456.com" target="_blank">޾Ʒ123߹ۿ</a>| <a href="http://eddiekidd.com" target="_blank">޾Ʒպרsilk</a>| <a href="http://wwwyy763.com" target="_blank">þ޾Ʒ</a>| <a href="http://wuiso.com" target="_blank">ŷ޾Ʒ</a>| <a href="http://18eeuus.com" target="_blank">ɫվwwwһ</a>| <a href="http://xianliwang.com" target="_blank">AVH߹ۿ</a>| <a href="http://www-ttyx.com" target="_blank">Ѱββ8x</a>| <a href="http://by6216.com" target="_blank">ձһۺϾþa</a>| <a href="http://www-095666.com" target="_blank">ѿƵ</a>| <a href="http://www045888.com" target="_blank">þþƷһ糱</a>| <a href="http://3baimm.com" target="_blank">ƷŮҹˬˬˬ</a>| <a href="http://www-171234.com" target="_blank">ƷѸ</a>| <a href="http://socgl.com" target="_blank">ѵҰսƵ</a>| <a href="http://516kd.com" target="_blank">www.ɫվ</a>| <a href="http://nibayuan.com" target="_blank">ëƬѲ</a>| <a href="http://ymtphoto.com" target="_blank">ƬƵۿ</a>| <a href="http://hylaowu.com" target="_blank">ִӲֻˬƵ </a>| <a href="http://av56cc.com" target="_blank">һ</a>| <a href="http://znboxcdn107.com" target="_blank">˳վ</a>| <a href="http://dd99com.com" target="_blank">һۿ</a>| <a href="http://0515zs.com" target="_blank">123Һ</a>| <a href="http://g8zb.com" target="_blank">ҹ޾Ʒ</a>| <a href="http://ytsp88.com" target="_blank">Ůվ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>