regular_expression_parser.html 6.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Regular Expression Parser</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <link href="theme/style.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  10. <tr>
  11. <td width="10" height="49"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>&nbsp;</b></font></td>
  12. <td width="85%" height="49"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Regular Expression Parser</b></font></td>
  13. <td width="112" height="49"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  14. </tr>
  15. </table>
  16. <br>
  17. <table border="0">
  18. <tr>
  19. <td width="10"></td>
  20. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  21. <td width="30"><a href="refactoring.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
  22. <td width="30"><a href="scoped_lock.html"><img src="theme/r_arr.gif" border="0"></a></td>
  23. </tr>
  24. </table>
  25. <p><a name="regular_expression_parser"></a>Regular expressions are a form of pattern-matching
  26. that are often used in text processing. Many users will be familiar with the
  27. usage of regular expressions. Initially there were the Unix utilities grep,
  28. sed and awk, and the programming language perl, each of which make extensive
  29. use of regular expressions. Today the usage of such regular expressions is integrated
  30. in many more available systems.</p>
  31. <p>During parser construction it is often useful to have the power of regular
  32. expressions available. The Regular Expression Parser was introduced, to make
  33. the use of regular expressions accessible for Spirit parser construction.</p>
  34. <p>The Regular Expression Parser <tt>rxstrlit</tt> has a single template type
  35. parameter: an iterator type. Internally, <tt>rxstrlit</tt> holds the Boost Regex
  36. object containing the provided regular expression. The <tt>rxstrlit</tt> attempts
  37. to match the current input stream with this regular expression. The template
  38. type parameter defaults to <tt>char const<span class="operators">*</span></tt>.
  39. <tt>rxstrlit</tt> has two constructors. The first accepts a null-terminated
  40. character pointer. This constructor may be used to build <tt>rxstrlit's</tt>
  41. from quoted regular expression literals. The second constructor takes in a first/last
  42. iterator pair. The function generator version is <tt>regex_p</tt>. </p>
  43. <p>Here are some examples:</p>
  44. <pre><code><span class=comment> </span><span class=identifier>rxstrlit</span><span class=special>&lt;&gt;(</span><span class=string>"Hello[[:space:]]+[W|w]orld"</span><span class=special>)
  45. </span><span class=identifier>regex_p</span><span class=special>(</span><span class=string>"Hello[[:space:]]+[W|w]orld"</span><span class=special>)
  46. </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=identifier>msg</span><span class=special>(</span><span class=string>"Hello[[:space:]]+[W|w]orld"</span><span class=special>);
  47. rx</span><span class=identifier>strlit</span><span class=special>&lt;&gt;(</span><span class=identifier>msg</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(), </span><span class=identifier>msg</span><span class=special>.</span><span class=identifier>end</span><span class=special>());</span></code></pre>
  48. <p>The generated parser object acts at the character level, thus an eventually
  49. given skip parser is not used during the attempt to match the regular expression
  50. (see <a href="faq.html#scanner_business">The Scanner Business</a>).</p>
  51. <p>The Regular Expression Parser is implemented by the help of the <a href="http://www.boost.org/libs/regex/index.html">Boost
  52. Regex++ library</a>, so you have to have some limitations in mind. </p>
  53. <blockquote>
  54. <p><img src="theme/bullet.gif" width="12" height="12"> Boost libraries have
  55. to be installed on your computer and the Boost root directory has to be added
  56. to your compiler <tt>#include&lt;...&gt;</tt> search path. You can download
  57. the actual version at the <a href="http://www.boost.org/">Boost web site</a>.</p>
  58. <p><img src="theme/bullet.gif" width="12" height="12"> The Boost Regex library
  59. requires the usage of bi-directional iterators. So you have to ensure this
  60. during the usage of the Spirit parser, which contains a Regular Expression
  61. Parser.</p>
  62. <p><img src="theme/bullet.gif" width="12" height="12"> The Boost Regex library
  63. is not a header only library, as Spirit is, though it provides the possibility
  64. to include all of the sources, if you are using it in one compilation unit
  65. only. Define the preprocessor constant <tt>BOOST_SPIRIT_NO_REGEX_LIB</tt> before
  66. including the spirit Regular Expression Parser header, if you want to include
  67. all the Boost Regex sources into this compilation unit. If you are using the
  68. Regular Expression Parser in more than one compilation unit, you should not
  69. define this constant and must link your application against the regex library
  70. as described in the related documentation.</p>
  71. </blockquote>
  72. <p> <img src="theme/lens.gif" width="15" height="16"> See <a href="../example/fundamental/regular_expression.cpp">regular_expression.cpp</a> for a compilable example. This is part of the Spirit distribution.</p>
  73. <table border="0">
  74. <tr>
  75. <td width="10"></td>
  76. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  77. <td width="30"><a href="refactoring.html"><img src="theme/l_arr.gif" width="20" height="19" border="0"></a></td>
  78. <td width="30"><a href="scoped_lock.html"><img src="theme/r_arr.gif" border="0"></a></td>
  79. </tr>
  80. </table>
  81. <br>
  82. <hr size="1">
  83. <p class="copyright">Copyright &copy; 2001-2002 Hartmut Kaiser<br>
  84. <br>
  85. <font size="2">Use, modification and distribution is subject to the Boost Software
  86. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  87. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  88. </body>
  89. </html>