PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/lpeg/re.html

https://bitbucket.org/missdeer/luapack
HTML | 344 lines | 320 code | 23 blank | 1 comment | 0 complexity | dfa74f28e9e6e15ceff24afa6566c5c3 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0, CC-BY-SA-3.0, ISC, LGPL-2.1, 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.11 2008/10/10 18:14:06 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>&lt;name&gt;</code></td><td>non terminal</td></tr>
  62. <tr><td><code>{}</code></td> <td>position capture</td></tr>
  63. <tr><td><code>{ p }</code></td> <td>simple capture</td></tr>
  64. <tr><td><code>{: p :}</code></td> <td>anonymous group capture</td></tr>
  65. <tr><td><code>{:name: p :}</code></td> <td>named group capture</td></tr>
  66. <tr><td><code>{~ p ~}</code></td> <td>substitution capture</td></tr>
  67. <tr><td><code>=name</code></td> <td>back reference
  68. </td></tr>
  69. <tr><td><code>p ?</code></td> <td>optional match</td></tr>
  70. <tr><td><code>p *</code></td> <td>zero or more repetitions</td></tr>
  71. <tr><td><code>p +</code></td> <td>one or more repetitions</td></tr>
  72. <tr><td><code>p^num</code></td> <td>exactly <code>n</code> repetitions</td></tr>
  73. <tr><td><code>p^+num</code></td>
  74. <td>at least <code>n</code> repetitions</td></tr>
  75. <tr><td><code>p^-num</code></td>
  76. <td>at most <code>n</code> repetitions</td></tr>
  77. <tr><td><code>p -&gt; 'string'</code></td> <td>string capture</td></tr>
  78. <tr><td><code>p -&gt; "string"</code></td> <td>string capture</td></tr>
  79. <tr><td><code>p -&gt; {}</code></td> <td>table capture</td></tr>
  80. <tr><td><code>p -&gt; name</code></td> <td>function/query/string capture
  81. equivalent to <code>p / defs[name]</code></td></tr>
  82. <tr><td><code>p =&gt; name</code></td> <td>match-time capture
  83. equivalent to <code>lpeg.Cmt(p, defs[name])</code></td></tr>
  84. <tr><td><code>& p</code></td> <td>and predicate</td></tr>
  85. <tr><td><code>! p</code></td> <td>not predicate</td></tr>
  86. <tr><td><code>p1 p2</code></td> <td>concatenation</td></tr>
  87. <tr><td><code>p1 / p2</code></td> <td>ordered choice</td></tr>
  88. <tr><td>(<code>name &lt;- p</code>)<sup>+</sup></td> <td>grammar</td></tr>
  89. </tbody></table>
  90. <p>
  91. Any space appearing in a syntax description can be
  92. replaced by zero or more space characters and Lua-style comments
  93. (<code>--</code> until end of line).
  94. </p>
  95. <p>
  96. Character classes define sets of characters.
  97. An initial <code>^</code> complements the resulting set.
  98. A range <em>x</em><code>-</code><em>y</em> includes in the set
  99. all characters with codes between the codes of <em>x</em> and <em>y</em>.
  100. A pre-defined class <code>%</code><em>name</em> includes all
  101. characters of that class.
  102. A simple character includes itself in the set.
  103. The only special characters inside a class are <code>^</code>
  104. (special only if it is the first character);
  105. <code>]</code>
  106. (can be included in the set as the first character,
  107. after the optional <code>^</code>);
  108. <code>%</code> (special only if followed by a letter);
  109. and <code>-</code>
  110. (can be included in the set as the first or the last character).
  111. </p>
  112. <p>
  113. Currently the pre-defined classes are similar to those from the
  114. Lua's string library
  115. (<code>%a</code> for letters,
  116. <code>%A</code> for non letters, etc.).
  117. There is also a class <code>%nl</code>
  118. containing only the newline character,
  119. which is particularly handy for grammars written inside long strings,
  120. as long strings do not interpret escape sequences like <code>\n</code>.
  121. </p>
  122. <h2><a name="func">Functions</a></h2>
  123. <h3><code>re.compile (string, [, defs])</code></h3>
  124. <p>
  125. Compiles the given string and
  126. returns an equivalent LPeg pattern.
  127. The given string may define either an expression or a grammar.
  128. The optional <code>defs</code> table provides extra Lua values
  129. to be used by the pattern.
  130. </p>
  131. <h3><code>re.find (subject, pattern [, init])</code></h3>
  132. <p>
  133. Searches the given pattern in the given subject.
  134. If it finds a match,
  135. returns the index where this occurrence starts,
  136. plus the captures made by the pattern (if any).
  137. Otherwise, returns nil.
  138. </p>
  139. <h3><code>re.match (subject, pattern)</code></h3>
  140. <p>
  141. Matches the given pattern against the given subject.
  142. </p>
  143. <h3><code>re.updatelocale ()</code></h3>
  144. <p>
  145. Updates the pre-defined character classes to the current locale.
  146. </p>
  147. <h2><a name="ex">Some Examples</a></h2>
  148. <h3>Balanced parentheses</h3>
  149. <p>
  150. As a simple example,
  151. the following call will produce the same pattern produced by the
  152. Lua expression in the
  153. <a href="lpeg.html#balanced">balanced parentheses</a> example:
  154. </p>
  155. <pre class="example">
  156. b = re.compile[[ balanced &lt;- "(" ([^()] / &lt;balanced&gt;)* ")" ]]
  157. </pre>
  158. <h3>String reversal</h3>
  159. <p>
  160. The next example reverses a string:
  161. </p>
  162. <pre class="example">
  163. rev = re.compile[[ R &lt;- (!.) -&gt; '' / ({.} &lt;R&gt;) -&gt; '%2%1']]
  164. print(rev:match"0123456789") --&gt; 9876543210
  165. </pre>
  166. <h3>CSV decoder</h3>
  167. <p>
  168. The next example replicates the <a href="lpeg.html#CSV">CSV decoder</a>:
  169. </p>
  170. <pre class="example">
  171. record = re.compile[[
  172. record &lt;- ( &lt;field&gt; (',' &lt;field&gt;)* ) -&gt; {} (%nl / !.)
  173. field &lt;- &lt;escaped&gt; / &lt;nonescaped&gt;
  174. nonescaped &lt;- { [^,"%nl]* }
  175. escaped &lt;- '"' {~ ([^"] / '""' -&gt; '"')* ~} '"'
  176. ]]
  177. </pre>
  178. <h3>Lua's long strings</h3>
  179. <p>
  180. The next example mathes Lua long strings:
  181. </p>
  182. <pre class="example">
  183. c = re.compile([[
  184. longstring &lt;- ('[' {:eq: '='* :} '[' &lt;close&gt;) =&gt; void
  185. close &lt;- ']' =eq ']' / . &lt;close&gt;
  186. ]], {void = function () return true end})
  187. print(c:match'[==[]]===]]]]==]===[]') --&gt; 17
  188. </pre>
  189. <h3>Indented blocks</h3>
  190. <p>
  191. This example breaks indented blocks into tables,
  192. respecting the indentation:
  193. </p>
  194. <pre class="example">
  195. p = re.compile[[
  196. block &lt;- ({:ident:' '*:} &lt;line&gt;
  197. ((=ident !' ' &lt;line&gt;) / &(=ident ' ') &lt;block&gt;)*) -&gt; {}
  198. line &lt;- {[^%nl]*} %nl
  199. ]]
  200. </pre>
  201. <p>
  202. As an example,
  203. consider the following text:
  204. </p>
  205. <pre class="example">
  206. t = p:match[[
  207. first line
  208. subline 1
  209. subline 2
  210. second line
  211. third line
  212. subline 3.1
  213. subline 3.1.1
  214. subline 3.2
  215. ]]
  216. </pre>
  217. <p>
  218. The resulting table <code>t</code> will be like this:
  219. </p>
  220. <pre class="example">
  221. {'first line'; {'subline 1'; 'subline 2'; ident = ' '};
  222. 'second line';
  223. 'third line'; { 'subline 3.1'; {'subline 3.1.1'; ident = ' '};
  224. 'subline 3.2'; ident = ' '};
  225. ident = ''}
  226. </pre>
  227. <h3>Macro expander</h3>
  228. <p>
  229. This example implements a simple macro expander.
  230. Macros must be defined as part of the pattern,
  231. following some simple rules:
  232. </p>
  233. <pre class="example">
  234. p = re.compile[[
  235. text &lt;- {~ &lt;item&gt;* ~}
  236. item &lt;- &lt;macro&gt; / [^()] / '(' &lt;item&gt;* ')'
  237. arg &lt;- ' '* {~ (!',' &lt;item&gt;)* ~}
  238. args &lt;- '(' &lt;arg&gt; (',' &lt;arg&gt;)* ')'
  239. -- now we define some macros
  240. macro &lt;- ('apply' &lt;args&gt;) -&gt; '%1(%2)'
  241. / ('add' &lt;args&gt;) -&gt; '%1 + %2'
  242. / ('mul' &lt;args&gt;) -&gt; '%1 * %2'
  243. ]]
  244. print(p:match"add(mul(a,b), apply(f,x))") --&gt; a * b + f(x)
  245. </pre>
  246. <p>
  247. A <code>text</code> is a sequence of items,
  248. wherein we apply a substitution capture to expand any macros.
  249. An <code>item</code> is either a macro,
  250. any character different from parentheses,
  251. or a parenthesized expression.
  252. A macro argument (<code>arg</code>) is a sequence
  253. of items different from a comma.
  254. (Note that a comma may appear inside an item,
  255. e.g., inside a parenthesized expression.)
  256. Again we do a substitution capture to expand any macro
  257. in the argument before expanding the outer macro.
  258. <code>args</code> is a list of arguments separated by commas.
  259. Finally we define the macros.
  260. Each macro is a string substitution;
  261. it replaces the macro name and its arguments by its corresponding string,
  262. with each <code>%</code><em>n</em> replaced by the <em>n</em>-th argument.
  263. </p>
  264. <h2><a name="license">License</a></h2>
  265. <p>
  266. Copyright &copy; 2008 Lua.org, PUC-Rio.
  267. </p>
  268. <p>
  269. Permission is hereby granted, free of charge,
  270. to any person obtaining a copy of this software and
  271. associated documentation files (the "Software"),
  272. to deal in the Software without restriction,
  273. including without limitation the rights to use,
  274. copy, modify, merge, publish, distribute, sublicense,
  275. and/or sell copies of the Software,
  276. and to permit persons to whom the Software is
  277. furnished to do so,
  278. subject to the following conditions:
  279. </p>
  280. <p>
  281. The above copyright notice and this permission notice
  282. shall be included in all copies or substantial portions of the Software.
  283. </p>
  284. <p>
  285. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  286. EXPRESS OR IMPLIED,
  287. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  288. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  289. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  290. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  291. TORT OR OTHERWISE, ARISING FROM,
  292. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  293. THE SOFTWARE.
  294. </p>
  295. </div> <!-- id="content" -->
  296. </div> <!-- id="main" -->
  297. <div id="about">
  298. <p><small>
  299. $Id: re.html,v 1.11 2008/10/10 18:14:06 roberto Exp $
  300. </small></p>
  301. </div> <!-- id="about" -->
  302. </div> <!-- id="container" -->
  303. </body>
  304. </html>