PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/MUSHclient/docs/re.html

http://aardwolfclientpackage.googlecode.com/
HTML | 418 lines | 375 code | 42 blank | 1 comment | 0 complexity | 4d8acbec5fc02a9e3af8c46d4845fa7e MD5 | raw file
Possible License(s): GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html>
  4. <head>
  5. <title>LPeg.re - Regex syntax for LPEG</title>
  6. <link rel="stylesheet"
  7. href="http://www.inf.puc-rio.br/~roberto/lpeg/doc.css"
  8. type="text/css"/>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  10. </head>
  11. <body>
  12. <!-- $Id: re.html,v 1.15 2010/11/05 12:53:43 roberto Exp $ -->
  13. <div id="container">
  14. <div id="product">
  15. <div id="product_logo">
  16. <a href="http://www.inf.puc-rio.br/~roberto/lpeg/">
  17. <img alt="LPeg logo" src="lpeg-128.gif"/>
  18. </a>
  19. </div>
  20. <div id="product_name"><big><strong>LPeg.re</strong></big></div>
  21. <div id="product_description">
  22. Regex syntax for LPEG
  23. </div>
  24. </div> <!-- id="product" -->
  25. <div id="main">
  26. <div id="navigation">
  27. <h1>re</h1>
  28. <ul>
  29. <li><a href="#basic">Basic Constructions</a></li>
  30. <li><a href="#func">Functions</a></li>
  31. <li><a href="#ex">Some Examples</a></li>
  32. <li><a href="#license">License</a></li>
  33. </ul>
  34. </li>
  35. </ul>
  36. </div> <!-- id="navigation" -->
  37. <div id="content">
  38. <h2><a name="basic"></a>The <code>re</code> Module</h2>
  39. <p>
  40. The <code>re</code> module
  41. (provided by file <code>re.lua</code> in the distribution)
  42. supports a somewhat conventional regex syntax
  43. for pattern usage within <a href="lpeg.html">LPeg</a>.
  44. </p>
  45. <p>
  46. The next table summarizes <code>re</code>'s syntax.
  47. A <code>p</code> represents an arbitrary pattern;
  48. <code>num</code> represents a number (<code>[0-9]+</code>);
  49. <code>name</code> represents an identifier
  50. (<code>[a-zA-Z][a-zA-Z0-9_]*</code>).
  51. Constructions are listed in order of decreasing precedence.
  52. <table border="1">
  53. <tbody><tr><td><b>Syntax</b></td><td><b>Description</b></td></tr>
  54. <tr><td><code>( p )</code></td> <td>grouping</td></tr>
  55. <tr><td><code>'string'</code></td> <td>literal string</td></tr>
  56. <tr><td><code>"string"</code></td> <td>literal string</td></tr>
  57. <tr><td><code>[class]</code></td> <td>character class</td></tr>
  58. <tr><td><code>.</code></td> <td>any character</td></tr>
  59. <tr><td><code>%name</code></td>
  60. <td>pattern <code>defs[name]</code> or a pre-defined pattern</td></tr>
  61. <tr><td><code>name</code></td><td>non terminal</td></tr>
  62. <tr><td><code>&lt;name&gt;</code></td><td>non terminal</td></tr>
  63. <tr><td><code>{}</code></td> <td>position capture</td></tr>
  64. <tr><td><code>{ p }</code></td> <td>simple capture</td></tr>
  65. <tr><td><code>{: p :}</code></td> <td>anonymous group capture</td></tr>
  66. <tr><td><code>{:name: p :}</code></td> <td>named group capture</td></tr>
  67. <tr><td><code>{~ p ~}</code></td> <td>substitution capture</td></tr>
  68. <tr><td><code>=name</code></td> <td>back reference
  69. </td></tr>
  70. <tr><td><code>p ?</code></td> <td>optional match</td></tr>
  71. <tr><td><code>p *</code></td> <td>zero or more repetitions</td></tr>
  72. <tr><td><code>p +</code></td> <td>one or more repetitions</td></tr>
  73. <tr><td><code>p^num</code></td> <td>exactly <code>n</code> repetitions</td></tr>
  74. <tr><td><code>p^+num</code></td>
  75. <td>at least <code>n</code> repetitions</td></tr>
  76. <tr><td><code>p^-num</code></td>
  77. <td>at most <code>n</code> repetitions</td></tr>
  78. <tr><td><code>p -&gt; 'string'</code></td> <td>string capture</td></tr>
  79. <tr><td><code>p -&gt; "string"</code></td> <td>string capture</td></tr>
  80. <tr><td><code>p -&gt; {}</code></td> <td>table capture</td></tr>
  81. <tr><td><code>p -&gt; name</code></td> <td>function/query/string capture
  82. equivalent to <code>p / defs[name]</code></td></tr>
  83. <tr><td><code>p =&gt; name</code></td> <td>match-time capture
  84. equivalent to <code>lpeg.Cmt(p, defs[name])</code></td></tr>
  85. <tr><td><code>& p</code></td> <td>and predicate</td></tr>
  86. <tr><td><code>! p</code></td> <td>not predicate</td></tr>
  87. <tr><td><code>p1 p2</code></td> <td>concatenation</td></tr>
  88. <tr><td><code>p1 / p2</code></td> <td>ordered choice</td></tr>
  89. <tr><td>(<code>name &lt;- p</code>)<sup>+</sup></td> <td>grammar</td></tr>
  90. </tbody></table>
  91. <p>
  92. Any space appearing in a syntax description can be
  93. replaced by zero or more space characters and Lua-style comments
  94. (<code>--</code> until end of line).
  95. </p>
  96. <p>
  97. Character classes define sets of characters.
  98. An initial <code>^</code> complements the resulting set.
  99. A range <em>x</em><code>-</code><em>y</em> includes in the set
  100. all characters with codes between the codes of <em>x</em> and <em>y</em>.
  101. A pre-defined class <code>%</code><em>name</em> includes all
  102. characters of that class.
  103. A simple character includes itself in the set.
  104. The only special characters inside a class are <code>^</code>
  105. (special only if it is the first character);
  106. <code>]</code>
  107. (can be included in the set as the first character,
  108. after the optional <code>^</code>);
  109. <code>%</code> (special only if followed by a letter);
  110. and <code>-</code>
  111. (can be included in the set as the first or the last character).
  112. </p>
  113. <p>
  114. Currently the pre-defined classes are similar to those from the
  115. Lua's string library
  116. (<code>%a</code> for letters,
  117. <code>%A</code> for non letters, etc.).
  118. There is also a class <code>%nl</code>
  119. containing only the newline character,
  120. which is particularly handy for grammars written inside long strings,
  121. as long strings do not interpret escape sequences like <code>\n</code>.
  122. </p>
  123. <h2><a name="func">Functions</a></h2>
  124. <h3><code>re.compile (string, [, defs])</code></h3>
  125. <p>
  126. Compiles the given string and
  127. returns an equivalent LPeg pattern.
  128. The given string may define either an expression or a grammar.
  129. The optional <code>defs</code> table provides extra Lua values
  130. to be used by the pattern.
  131. </p>
  132. <h3><code>re.find (subject, pattern [, init])</code></h3>
  133. <p>
  134. Searches the given pattern in the given subject.
  135. If it finds a match,
  136. returns the index where this occurrence starts,
  137. plus the captures made by the pattern (if any).
  138. Otherwise, returns nil.
  139. </p>
  140. <p>
  141. An optional numeric argument <code>init</code> makes the search
  142. starts at that position in the subject string.
  143. As usual in Lua libraries,
  144. a negative value counts from the end.
  145. </p>
  146. <h3><code>re.match (subject, pattern)</code></h3>
  147. <p>
  148. Matches the given pattern against the given subject.
  149. </p>
  150. <h3><code>re.updatelocale ()</code></h3>
  151. <p>
  152. Updates the pre-defined character classes to the current locale.
  153. </p>
  154. <h2><a name="ex">Some Examples</a></h2>
  155. <h3>A complete simple program</h3>
  156. <p>
  157. The next code shows a simple complete Lua program using
  158. the <code>re</code> module:
  159. </p>
  160. <pre class="example">
  161. local re = require"re"
  162. -- find the position of the first number in a string
  163. print(re.find("the number 423 is odd", "[0-9]+")) --&gt; 12
  164. -- similar, but also captures (and returns) the number
  165. print(re.find("the number 423 is odd", "{[0-9]+}")) --&gt; 12 423
  166. -- returns all words in a string
  167. print(re.match("the number 423 is odd", "({%a+} / .)*"))
  168. --&gt; the number is odd
  169. </pre>
  170. <h3>Balanced parentheses</h3>
  171. <p>
  172. The following call will produce the same pattern produced by the
  173. Lua expression in the
  174. <a href="lpeg.html#balanced">balanced parentheses</a> example:
  175. </p>
  176. <pre class="example">
  177. b = re.compile[[ balanced &lt;- "(" ([^()] / balanced)* ")" ]]
  178. </pre>
  179. <h3>String reversal</h3>
  180. <p>
  181. The next example reverses a string:
  182. </p>
  183. <pre class="example">
  184. rev = re.compile[[ R &lt;- (!.) -&gt; '' / ({.} R) -&gt; '%2%1']]
  185. print(rev:match"0123456789") --&gt; 9876543210
  186. </pre>
  187. <h3>CSV decoder</h3>
  188. <p>
  189. The next example replicates the <a href="lpeg.html#CSV">CSV decoder</a>:
  190. </p>
  191. <pre class="example">
  192. record = re.compile[[
  193. record &lt;- ( field (',' field)* ) -&gt; {} (%nl / !.)
  194. field &lt;- escaped / nonescaped
  195. nonescaped &lt;- { [^,"%nl]* }
  196. escaped &lt;- '"' {~ ([^"] / '""' -&gt; '"')* ~} '"'
  197. ]]
  198. </pre>
  199. <h3>Lua's long strings</h3>
  200. <p>
  201. The next example matches Lua long strings:
  202. </p>
  203. <pre class="example">
  204. c = re.compile([[
  205. longstring &lt;- ('[' {:eq: '='* :} '[' close) -&gt; void
  206. close &lt;- ']' =eq ']' / . close
  207. ]], {void = function () end})
  208. print(c:match'[==[]]===]]]]==]===[]') --&gt; 17
  209. </pre>
  210. <h3>Indented blocks</h3>
  211. <p>
  212. This example breaks indented blocks into tables,
  213. respecting the indentation:
  214. </p>
  215. <pre class="example">
  216. p = re.compile[[
  217. block &lt;- ({:ident:' '*:} line
  218. ((=ident !' ' line) / &(=ident ' ') block)*) -&gt; {}
  219. line &lt;- {[^%nl]*} %nl
  220. ]]
  221. </pre>
  222. <p>
  223. As an example,
  224. consider the following text:
  225. </p>
  226. <pre class="example">
  227. t = p:match[[
  228. first line
  229. subline 1
  230. subline 2
  231. second line
  232. third line
  233. subline 3.1
  234. subline 3.1.1
  235. subline 3.2
  236. ]]
  237. </pre>
  238. <p>
  239. The resulting table <code>t</code> will be like this:
  240. </p>
  241. <pre class="example">
  242. {'first line'; {'subline 1'; 'subline 2'; ident = ' '};
  243. 'second line';
  244. 'third line'; { 'subline 3.1'; {'subline 3.1.1'; ident = ' '};
  245. 'subline 3.2'; ident = ' '};
  246. ident = ''}
  247. </pre>
  248. <h3>Macro expander</h3>
  249. <p>
  250. This example implements a simple macro expander.
  251. Macros must be defined as part of the pattern,
  252. following some simple rules:
  253. </p>
  254. <pre class="example">
  255. p = re.compile[[
  256. text &lt;- {~ item* ~}
  257. item &lt;- macro / [^()] / '(' item* ')'
  258. arg &lt;- ' '* {~ (!',' item)* ~}
  259. args &lt;- '(' arg (',' arg)* ')'
  260. -- now we define some macros
  261. macro &lt;- ('apply' args) -&gt; '%1(%2)'
  262. / ('add' args) -&gt; '%1 + %2'
  263. / ('mul' args) -&gt; '%1 * %2'
  264. ]]
  265. print(p:match"add(mul(a,b), apply(f,x))") --&gt; a * b + f(x)
  266. </pre>
  267. <p>
  268. A <code>text</code> is a sequence of items,
  269. wherein we apply a substitution capture to expand any macros.
  270. An <code>item</code> is either a macro,
  271. any character different from parentheses,
  272. or a parenthesized expression.
  273. A macro argument (<code>arg</code>) is a sequence
  274. of items different from a comma.
  275. (Note that a comma may appear inside an item,
  276. e.g., inside a parenthesized expression.)
  277. Again we do a substitution capture to expand any macro
  278. in the argument before expanding the outer macro.
  279. <code>args</code> is a list of arguments separated by commas.
  280. Finally we define the macros.
  281. Each macro is a string substitution;
  282. it replaces the macro name and its arguments by its corresponding string,
  283. with each <code>%</code><em>n</em> replaced by the <em>n</em>-th argument.
  284. </p>
  285. <h3>Patterns</h3>
  286. <p>
  287. This example shows the complete syntax
  288. of patterns accepted by <code>re</code>.
  289. </p>
  290. <pre class="example">
  291. p = [=[
  292. pattern &lt;- exp !.
  293. exp &lt;- S (alternative / grammar)
  294. alternative &lt;- seq ('/' S seq)*
  295. seq &lt;- prefix*
  296. prefix &lt;- '&amp;' S prefix / '!' S prefix / suffix
  297. suffix &lt;- primary S (([+*?]
  298. / '^' [+-]? num
  299. / '-&gt;' S (string / '{}' / name)
  300. / '=&gt;' S name) S)*
  301. primary &lt;- '(' exp ')' / string / class / defined
  302. / '{:' (name ':')? exp ':}'
  303. / '=' name
  304. / '{}'
  305. / '{~' exp '~}'
  306. / '{' exp '}'
  307. / '.'
  308. / name S !arrow
  309. / '&lt;' name '&gt;' -- old-style non terminals
  310. grammar &lt;- definition+
  311. definition &lt;- name S arrow exp
  312. class &lt;- '[' '^'? item (!']' item)* ']'
  313. item &lt;- defined / range / .
  314. range &lt;- . '-' [^]]
  315. S &lt;- (%s / '--' [^%nl]*)* -- spaces and comments
  316. name &lt;- [A-Za-z][A-Za-z0-9_]*
  317. arrow &lt;- '&lt;-'
  318. num &lt;- [0-9]+
  319. string &lt;- '"' [^"]* '"' / "'" [^']* "'"
  320. defined &lt;- '%' name
  321. ]=]
  322. print(re.match(p, p)) -- a self description must match itself
  323. </pre>
  324. <h2><a name="license">License</a></h2>
  325. <p>
  326. Copyright &copy; 2008-2010 Lua.org, PUC-Rio.
  327. </p>
  328. <p>
  329. Permission is hereby granted, free of charge,
  330. to any person obtaining a copy of this software and
  331. associated documentation files (the "Software"),
  332. to deal in the Software without restriction,
  333. including without limitation the rights to use,
  334. copy, modify, merge, publish, distribute, sublicense,
  335. and/or sell copies of the Software,
  336. and to permit persons to whom the Software is
  337. furnished to do so,
  338. subject to the following conditions:
  339. </p>
  340. <p>
  341. The above copyright notice and this permission notice
  342. shall be included in all copies or substantial portions of the Software.
  343. </p>
  344. <p>
  345. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  346. EXPRESS OR IMPLIED,
  347. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  348. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  349. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  350. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  351. TORT OR OTHERWISE, ARISING FROM,
  352. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  353. THE SOFTWARE.
  354. </p>
  355. </div> <!-- id="content" -->
  356. </div> <!-- id="main" -->
  357. <div id="about">
  358. <p><small>
  359. $Id: re.html,v 1.15 2010/11/05 12:53:43 roberto Exp $
  360. </small></p>
  361. </div> <!-- id="about" -->
  362. </div> <!-- id="container" -->
  363. </body>
  364. </html>