这两个正则表达式有什么区别?
What's the difference between these two regular expressions?
问题的简短版本是:为什么这两个正则表达式不同?即,
href=(['"]).+?
对
href=(['"]).+?['"]
或 href=(['"]).+?(['"])
我正在这个网站上练习正则表达式,我正在尝试解决这个问题
http://play.inginf.units.it/#/level/6
我将全部内容张贴在这里以防将来网站出现故障。
<tr>
<a href="javascript:openurl('/Xplore/accessinfo.jsp')" class="topUnderlineLinks">
<A href="/iel5/4235/4079606/04079617.pdf?tp=&arnumber=4079617&isnumber=4079606" class="bodyCopy">PDF</A>(3141 KB)
<A href='/xpl/RecentCon.jsp?punumber=10417'>Evolutionary Computation, 2005. The 2005 IEEE Congress on</A><br>
<td width="33%" ><div align="right"> <a href="/xplorehelp/Help_start.html#Help_searchresults.html" class="subNavLinks" target="blank">Help</a> <a href="/xpl/contactus.jsp" class="subNavLinks">Contact
Kimya ile ilgili çeþitli temel referans
<a href="http://search.epnet.com/login.asp?profile=web&defaultdb=geh"
<a href="http://iimpft.chadwyck.com/" target="_parent">International
<a href="standartlar.html#tse" target="_parent">NFPA Standartlarý</a>
<a href="http://www.gutenberg.org/" target="_parent">Project Gutenberg</a>
<a href="http://proquestcombo.safaribooksonline.com/?portal=proquestcombo&uicode=istanbultek"
<a href="http://www.scitation.org" target="_parent">Scitation</a>
dergilerin listesini görmek için <a href="/online/aip.html">bu yolu</a>
<a href="http://www3.interscience.wiley.com/journalfinder.html"
<td width="46%"><a href="/xpl/periodicals.jsp" class="dropDownNav" accesskey="j">Journals & Magazines
<td><a href="http://www.ieee.org/products/onlinepubs/resources/XploreTutorial.pdf" class="dropDownNav">IEEE Xplore Demo</a></td>
| <a href="/xpl/tocalerts_signup.jsp" class="topUnderlineLinks">Alerts</a>
<A href='/xpl/RecentCon.jsp?punumber=10417'>Evolutionary Computation, 2005. The 2005 IEEE Congress on</A><br>
<a href="/search/srchabstract.jsp?arnumber=1554748&isnumber=33079&punumber=10417&k2dockey=1554748@ieeecnfs&query=%28+grammatical+evolution%3Cin%3Eti+%29&pos=9" class="bodyCopy">Abstract</a>
<td><a href="history.jsp">View Session History</a></td>
<td><a href="advsearch.jsp">New Search</a></td>
<a href="http://web5s.silverplatter.com/webspirs/start.ws?customer=kaynak"
<a href="standartlar.html#tse">Türk Standartlarý</a>
<a href="http://isiknowledge.com" target="_parent">Web of Science</a>
<a href='deneme.html#bg'>Butler Group </a>veritabanýna 31 Mart 2007 tarihine kadar deneme eriþimi alýnmýþtýr. <span class="tarih">(19.03.2007)</span>
<a href='deneme.html#ps'>Productscan</a> veritabanýna 31 Mart 2007 tarihine kadar deneme eriþimi alýnmýþtýr. <span class="tarih">(19.03.2007)</span>
我应该像这样匹配文本
href="history.jsp"
也就是说,我需要匹配上面文本中的任何 href。
现在根据Solutions,这个问题的答案似乎是href=(['"]).+?
但是最后一个反向引用,如果我不使用它并重复正则表达式组(我希望括号称为组,如果我错了请纠正我),为什么我会得到不同的结果?也就是说,如果我使用它,我会得到错误的结果。 href=(['"]).+?['"]
或 href=(['"]).+?(['"])
反向引用必须与捕获组匹配的内容相匹配。所以第一个正则表达式将匹配
"abcd"
或
'abcd'
第二个版本没有link匹配的两端,所以它也会匹配以下内容:
"abcd'
或
'abcd"
因此带有反向引用的版本只匹配被相同类型的引号包围的字符串。
如果您在字符串中嵌入引号,例如
,这种区别就很重要
some text "<div id='foo'>" more text
带有反向引用的版本将匹配 "<div id='foo'>"
,但没有反向引用的版本将匹配 "<div id='
。
正则表达式片段 (['"]).+?
使用 (...)
捕获开头引号,并使用反向引用稍后在 </code> 中使用它。这意味着 <code>'xyzzy'
或 "plugh"
会匹配,但 不会 'twisty"
.
这可能是正确的形式,因为使用 (['"]).+?['"]
,它可以用任一引号打开和关闭。
顺便说一句,在你的后一个表达式中捕获组没有什么意义,除非你打算以某种方式在代码中使用它们。如果您捕获了 两者, 您可以检查以确保它们相同,但这可能最好通过使用反向引用版本来处理。
换句话说,如果你想允许像 'twisty"
这样的东西,你只需要 ['"].+?['"]
.
问题的简短版本是:为什么这两个正则表达式不同?即,
href=(['"]).+?
对
href=(['"]).+?['"]
或 href=(['"]).+?(['"])
我正在这个网站上练习正则表达式,我正在尝试解决这个问题
http://play.inginf.units.it/#/level/6
我将全部内容张贴在这里以防将来网站出现故障。
<tr>
<a href="javascript:openurl('/Xplore/accessinfo.jsp')" class="topUnderlineLinks">
<A href="/iel5/4235/4079606/04079617.pdf?tp=&arnumber=4079617&isnumber=4079606" class="bodyCopy">PDF</A>(3141 KB)
<A href='/xpl/RecentCon.jsp?punumber=10417'>Evolutionary Computation, 2005. The 2005 IEEE Congress on</A><br>
<td width="33%" ><div align="right"> <a href="/xplorehelp/Help_start.html#Help_searchresults.html" class="subNavLinks" target="blank">Help</a> <a href="/xpl/contactus.jsp" class="subNavLinks">Contact
Kimya ile ilgili çeþitli temel referans
<a href="http://search.epnet.com/login.asp?profile=web&defaultdb=geh"
<a href="http://iimpft.chadwyck.com/" target="_parent">International
<a href="standartlar.html#tse" target="_parent">NFPA Standartlarý</a>
<a href="http://www.gutenberg.org/" target="_parent">Project Gutenberg</a>
<a href="http://proquestcombo.safaribooksonline.com/?portal=proquestcombo&uicode=istanbultek"
<a href="http://www.scitation.org" target="_parent">Scitation</a>
dergilerin listesini görmek için <a href="/online/aip.html">bu yolu</a>
<a href="http://www3.interscience.wiley.com/journalfinder.html"
<td width="46%"><a href="/xpl/periodicals.jsp" class="dropDownNav" accesskey="j">Journals & Magazines
<td><a href="http://www.ieee.org/products/onlinepubs/resources/XploreTutorial.pdf" class="dropDownNav">IEEE Xplore Demo</a></td>
| <a href="/xpl/tocalerts_signup.jsp" class="topUnderlineLinks">Alerts</a>
<A href='/xpl/RecentCon.jsp?punumber=10417'>Evolutionary Computation, 2005. The 2005 IEEE Congress on</A><br>
<a href="/search/srchabstract.jsp?arnumber=1554748&isnumber=33079&punumber=10417&k2dockey=1554748@ieeecnfs&query=%28+grammatical+evolution%3Cin%3Eti+%29&pos=9" class="bodyCopy">Abstract</a>
<td><a href="history.jsp">View Session History</a></td>
<td><a href="advsearch.jsp">New Search</a></td>
<a href="http://web5s.silverplatter.com/webspirs/start.ws?customer=kaynak"
<a href="standartlar.html#tse">Türk Standartlarý</a>
<a href="http://isiknowledge.com" target="_parent">Web of Science</a>
<a href='deneme.html#bg'>Butler Group </a>veritabanýna 31 Mart 2007 tarihine kadar deneme eriþimi alýnmýþtýr. <span class="tarih">(19.03.2007)</span>
<a href='deneme.html#ps'>Productscan</a> veritabanýna 31 Mart 2007 tarihine kadar deneme eriþimi alýnmýþtýr. <span class="tarih">(19.03.2007)</span>
我应该像这样匹配文本
href="history.jsp"
也就是说,我需要匹配上面文本中的任何 href。
现在根据Solutions,这个问题的答案似乎是href=(['"]).+?
但是最后一个反向引用,如果我不使用它并重复正则表达式组(我希望括号称为组,如果我错了请纠正我),为什么我会得到不同的结果?也就是说,如果我使用它,我会得到错误的结果。 href=(['"]).+?['"]
或 href=(['"]).+?(['"])
反向引用必须与捕获组匹配的内容相匹配。所以第一个正则表达式将匹配
"abcd"
或
'abcd'
第二个版本没有link匹配的两端,所以它也会匹配以下内容:
"abcd'
或
'abcd"
因此带有反向引用的版本只匹配被相同类型的引号包围的字符串。
如果您在字符串中嵌入引号,例如
,这种区别就很重要some text "<div id='foo'>" more text
带有反向引用的版本将匹配 "<div id='foo'>"
,但没有反向引用的版本将匹配 "<div id='
。
正则表达式片段 (['"]).+?
使用 (...)
捕获开头引号,并使用反向引用稍后在 </code> 中使用它。这意味着 <code>'xyzzy'
或 "plugh"
会匹配,但 不会 'twisty"
.
这可能是正确的形式,因为使用 (['"]).+?['"]
,它可以用任一引号打开和关闭。
顺便说一句,在你的后一个表达式中捕获组没有什么意义,除非你打算以某种方式在代码中使用它们。如果您捕获了 两者, 您可以检查以确保它们相同,但这可能最好通过使用反向引用版本来处理。
换句话说,如果你想允许像 'twisty"
这样的东西,你只需要 ['"].+?['"]
.