| 1 |
<html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 |
<title>Regular expressions 4. Special constructs with parentheses</title> |
| 3 |
<style> |
| 4 |
td { padding-left: 5pt; padding-right: 5pt; } |
| 5 |
th { padding-left: 5pt; padding-right: 5pt; } |
| 6 |
code { white-space: pre-wrap; } |
| 7 |
dd { white-space: pre-wrap; } |
| 8 |
</style> |
| 9 |
</head><body bgcolor="#FFFFFF"> |
| 10 |
|
| 11 |
<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 |
Regular expressions 4. Special constructs with parentheses |
| 13 |
</b></font></table></table> |
| 14 |
<p>Some special constructs exist with parentheses. </p> |
| 15 |
<dl> |
| 16 |
<dt><b>(?:</b><i>regex</i><b></b>)<b></b> is a grouping-only construct. |
| 17 |
<dd>They exist merely for efficiency reasons and facilitate grouping.</dd> |
| 18 |
<dt><b>(?=</b><i>regex</i><b></b>)<b></b> is a positive look-ahead. |
| 19 |
<dd>A match of the regular expression contained in the positive look-ahead construct is attempted. If the match succeeds, control is passed to the regex following this construct and the text consumed by this look-ahead construct is first unmatched. </dd> |
| 20 |
<dt><b>(?!</b><i>regex</i><b></b>)<b></b> is a negative look-ahead. |
| 21 |
<dd>Functions like a positive look-ahead, only the <i>regex</i> must <i>not</i> match.</dd> |
| 22 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">Example: "abc(?!.*abc.*)" searches for the <i>last</i> occurrence of "abc" in a string.</dd> |
| 23 |
<dt><b>(?i</b><i>regex</i><b></b>)<b></b> is a case insensitive regex. |
| 24 |
<dt><b>(?I</b><i>regex</i><b></b>)<b></b> is a case sensitive regex. |
| 25 |
<dd>Default a regex is case sensitive. </dd> |
| 26 |
<dd> Example: "(?iaa)" matches "aa", "aA", "Aa" and "AA".</dd> |
| 27 |
<dt><b>(?n</b><i>regex</i><b></b>)<b></b> matches newlines. |
| 28 |
<dt><b>(?N</b><i>regex</i><b></b>)<b></b> doesn't match newlines. |
| 29 |
</dl> |
| 30 |
<p>All the constructs above do not capture text and cannot be referenced, i.e., the parentheses are not counted. However, you can make them capture text by surrounding them with <i>ordinary</i> parentheses.</p> |
| 31 |
<h3>Links to this page</h3> |
| 32 |
<ul> |
| 33 |
<li><a href="Regular_expressions.html">Regular expressions</a> |
| 34 |
<li><a href="Regular_expressions_1__Special_characters.html">Regular expressions 1. Special characters</a> |
| 35 |
</ul> |
| 36 |
<hr> |
| 37 |
<address> |
| 38 |
<p>© djmw 20010710</p> |
| 39 |
</address> |
| 40 |
</body> |
| 41 |
</html> |