1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_c_io.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_method.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class MatchData</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#_ob_cb"><i>[ ]</i></a> <a href="#begin"><i>begin</i></a> <a href="#end"><i>end</i></a> <a href="#length"><i>length</i></a> <a href="#offset"><i>offset</i></a> <a href="#post_match"><i>post_match</i></a> <a href="#pre_match"><i>pre_match</i></a> <a href="#size"><i>size</i></a> <a href="#string"><i>string</i></a> <a href="#to_a"><i>to_a</i></a> <a href="#to_s"><i>to_s</i></a> <p></p><hr>
<P></P>
<code>MatchData</code> is the type of the special variable <code>$~</code>,
and is the type of the object returned by <a href="ref_c_regexp.html#match"><code>Regexp#match</code></a> and
<a href="ref_c_regexp.html#last_match"><code>Regexp#last_match</code></a>. It
encapsulates all the results of a pattern match, results normally
accessed through the special variables <code>$&</code>, <code>$'</code>,
<code>$`</code>, <code>$1</code>, <code>$2</code>, and so on.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>[<i>i</i>] -> <i>anObject</i><br><i>mtch</i>[<i>start</i>, <i>length</i>] -> <i>anArray</i><br><i>mtch</i>[<i>aRange</i>] -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Match Reference---<code>MatchData</code> acts as an array, and may
be accessed using the normal array indexing techniques.
<i>mtch</i>[0] is equivalent to the special variable <code>$&</code>, and
returns the entire matched string. <i>mtch</i>[1], <i>mtch</i>[2], and so
on return the
values of the matched backreferences (portions of the pattern
between parentheses).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m[0]</code></td>
<td valign="top"></td>
<td valign="top"><code>"HX1138"</code></td>
</tr>
<tr>
<td valign="top"><code>m[1, 2]</code></td>
<td valign="top"></td>
<td valign="top"><code>["H", "X"]</code></td>
</tr>
<tr>
<td valign="top"><code>m[1..3]</code></td>
<td valign="top"></td>
<td valign="top"><code>["H", "X", "113"]</code></td>
</tr>
<tr>
<td valign="top"><code>m[-3, 2]</code></td>
<td valign="top"></td>
<td valign="top"><code>["X", "113"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="begin"><b>begin</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.begin( <i>n</i> ) -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Returns the offset of the start of the <em>n</em>th element of the
match array in the string.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.begin(0)</code></td>
<td valign="top"></td>
<td valign="top"><code>1</code></td>
</tr>
<tr>
<td valign="top"><code>m.begin(2)</code></td>
<td valign="top"></td>
<td valign="top"><code>2</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="end"><b>end</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.end( <i>n</i> ) -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Returns the offset of the character immediately following the
end of the <em>n</em>th element of the match array in the string.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.end(0)</code></td>
<td valign="top"></td>
<td valign="top"><code>7</code></td>
</tr>
<tr>
<td valign="top"><code>m.end(2)</code></td>
<td valign="top"></td>
<td valign="top"><code>3</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="length"><b>length</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.length -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Returns the number of elements in the match array.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.length</code></td>
<td valign="top"></td>
<td valign="top"><code>5</code></td>
</tr>
<tr>
<td valign="top"><code>m.size</code></td>
<td valign="top"></td>
<td valign="top"><code>5</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="offset"><b>offset</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.offset( <i>n</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a two-element array containing the beginning and ending
offsets of the <em>n</em>th match.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.offset(0)</code></td>
<td valign="top"></td>
<td valign="top"><code>[1, 7]</code></td>
</tr>
<tr>
<td valign="top"><code>m.offset(4)</code></td>
<td valign="top"></td>
<td valign="top"><code>[6, 7]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="post_match"><b>post_match</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.post_match -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the portion of the original string after the current
match. Equivalent to the special variable <code>$'</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")</code></td>
</tr>
<tr>
<td valign="top"><code>m.post_match</code></td>
<td valign="top"></td>
<td valign="top"><code>": The Movie"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="pre_match"><b>pre_match</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.pre_match -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the portion of the original string before the current
match. Equivalent to the special variable <code>$`</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.pre_match</code></td>
<td valign="top"></td>
<td valign="top"><code>"T"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="size"><b>size</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.size -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
A synonym for <a href="ref_c_matchdata.html#length"><code>MatchData#length</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="string"><b>string</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.string -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns a frozen copy of the string passed in to <code>match</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.string</code></td>
<td valign="top"></td>
<td valign="top"><code>"THX1138."</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_a"><b>to_a</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.to_a -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the array of matches.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.to_a</code></td>
<td valign="top"></td>
<td valign="top"><code>["HX1138", "H", "X", "113", "8"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_s"><b>to_s</b></a></font></td><td bgcolor="#ffaaaa">
<i>mtch</i>.to_s -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the entire matched string.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>m = /(.)(.)(\d+)(\d)/.match("THX1138.")</code></td>
</tr>
<tr>
<td valign="top"><code>m.to_s</code></td>
<td valign="top"></td>
<td valign="top"><code>"HX1138"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_c_io.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_method.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|