intro.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introduction and Overview</title>
  5. <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Boost.Regex 5.1.4">
  8. <link rel="up" href="../index.html" title="Boost.Regex 5.1.4">
  9. <link rel="prev" href="install.html" title="Building and Installing the Library">
  10. <link rel="next" href="unicode.html" title="Unicode and Boost.Regex">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="install.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unicode.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="boost_regex.intro"></a><a class="link" href="intro.html" title="Introduction and Overview">Introduction and Overview</a>
  28. </h2></div></div></div>
  29. <p>
  30. Regular expressions are a form of pattern-matching that are often used in text
  31. processing; many users will be familiar with the Unix utilities grep, sed and
  32. awk, and the programming language Perl, each of which make extensive use of
  33. regular expressions. Traditionally C++ users have been limited to the POSIX
  34. C API's for manipulating regular expressions, and while Boost.Regex does provide
  35. these API's, they do not represent the best way to use the library. For example
  36. Boost.Regex can cope with wide character strings, or search and replace operations
  37. (in a manner analogous to either sed or Perl), something that traditional C
  38. libraries can not do.
  39. </p>
  40. <p>
  41. The class <a class="link" href="ref/basic_regex.html" title="basic_regex"><code class="computeroutput"><span class="identifier">basic_regex</span></code></a>
  42. is the key class in this library; it represents a "machine readable"
  43. regular expression, and is very closely modeled on <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code>,
  44. think of it as a string plus the actual state-machine required by the regular
  45. expression algorithms. Like <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code>
  46. there are two typedefs that are almost always the means by which this class
  47. is referenced:
  48. </p>
  49. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span>
  50. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">charT</span><span class="special">,</span>
  51. <span class="keyword">class</span> <span class="identifier">traits</span> <span class="special">=</span> <span class="identifier">regex_traits</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;</span> <span class="special">&gt;</span>
  52. <span class="keyword">class</span> <span class="identifier">basic_regex</span><span class="special">;</span>
  53. <span class="keyword">typedef</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">regex</span><span class="special">;</span>
  54. <span class="keyword">typedef</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="keyword">wchar_t</span><span class="special">&gt;</span> <span class="identifier">wregex</span><span class="special">;</span>
  55. <span class="special">}</span>
  56. </pre>
  57. <p>
  58. To see how this library can be used, imagine that we are writing a credit card
  59. processing application. Credit card numbers generally come as a string of 16-digits,
  60. separated into groups of 4-digits, and separated by either a space or a hyphen.
  61. Before storing a credit card number in a database (not necessarily something
  62. your customers will appreciate!), we may want to verify that the number is
  63. in the correct format. To match any digit we could use the regular expression
  64. [0-9], however ranges of characters like this are actually locale dependent.
  65. Instead we should use the POSIX standard form [[:digit:]], or the Boost.Regex
  66. and Perl shorthand for this \d (note that many older libraries tended to be
  67. hard-coded to the C-locale, consequently this was not an issue for them). That
  68. leaves us with the following regular expression to validate credit card number
  69. formats:
  70. </p>
  71. <pre class="programlisting">(\d{4}[- ]){3}\d{4}</pre>
  72. <p>
  73. Here the parenthesis act to group (and mark for future reference) sub-expressions,
  74. and the {4} means "repeat exactly 4 times". This is an example of
  75. the extended regular expression syntax used by Perl, awk and egrep. Boost.Regex
  76. also supports the older "basic" syntax used by sed and grep, but
  77. this is generally less useful, unless you already have some basic regular expressions
  78. that you need to reuse.
  79. </p>
  80. <p>
  81. Now let's take that expression and place it in some C++ code to validate the
  82. format of a credit card number:
  83. </p>
  84. <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">validate_card_format</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">s</span><span class="special">)</span>
  85. <span class="special">{</span>
  86. <span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e</span><span class="special">(</span><span class="string">"(\\d{4}[- ]){3}\\d{4}"</span><span class="special">);</span>
  87. <span class="keyword">return</span> <span class="identifier">regex_match</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">e</span><span class="special">);</span>
  88. <span class="special">}</span>
  89. </pre>
  90. <p>
  91. Note how we had to add some extra escapes to the expression: remember that
  92. the escape is seen once by the C++ compiler, before it gets to be seen by the
  93. regular expression engine, consequently escapes in regular expressions have
  94. to be doubled up when embedding them in C/C++ code. Also note that all the
  95. examples assume that your compiler supports argument-dependent lookup, if yours
  96. doesn't (for example VC6), then you will have to add some <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span></code> prefixes to some of the function calls in
  97. the examples.
  98. </p>
  99. <p>
  100. Those of you who are familiar with credit card processing, will have realized
  101. that while the format used above is suitable for human readable card numbers,
  102. it does not represent the format required by online credit card systems; these
  103. require the number as a string of 16 (or possibly 15) digits, without any intervening
  104. spaces. What we need is a means to convert easily between the two formats,
  105. and this is where search and replace comes in. Those who are familiar with
  106. the utilities sed and Perl will already be ahead here; we need two strings
  107. - one a regular expression - the other a "format string" that provides
  108. a description of the text to replace the match with. In Boost.Regex this search
  109. and replace operation is performed with the algorithm <a class="link" href="ref/regex_replace.html" title="regex_replace"><code class="computeroutput"><span class="identifier">regex_replace</span></code></a>, for our credit card
  110. example we can write two algorithms like this to provide the format conversions:
  111. </p>
  112. <pre class="programlisting"><span class="comment">// match any format with the regular expression:</span>
  113. <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e</span><span class="special">(</span><span class="string">"\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"</span><span class="special">);</span>
  114. <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">machine_format</span><span class="special">(</span><span class="string">"\\1\\2\\3\\4"</span><span class="special">);</span>
  115. <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">human_format</span><span class="special">(</span><span class="string">"\\1-\\2-\\3-\\4"</span><span class="special">);</span>
  116. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">machine_readable_card_number</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">)</span>
  117. <span class="special">{</span>
  118. <span class="keyword">return</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">machine_format</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_default</span> <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">format_sed</span><span class="special">);</span>
  119. <span class="special">}</span>
  120. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">human_readable_card_number</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">)</span>
  121. <span class="special">{</span>
  122. <span class="keyword">return</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">human_format</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_default</span> <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">format_sed</span><span class="special">);</span>
  123. <span class="special">}</span>
  124. </pre>
  125. <p>
  126. Here we've used marked sub-expressions in the regular expression to split out
  127. the four parts of the card number as separate fields, the format string then
  128. uses the sed-like syntax to replace the matched text with the reformatted version.
  129. </p>
  130. <p>
  131. In the examples above, we haven't directly manipulated the results of a regular
  132. expression match, however in general the result of a match contains a number
  133. of sub-expression matches in addition to the overall match. When the library
  134. needs to report a regular expression match it does so using an instance of
  135. the class <a class="link" href="ref/match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a>,
  136. as before there are typedefs of this class for the most common cases:
  137. </p>
  138. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span>
  139. <span class="keyword">typedef</span> <span class="identifier">match_results</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">char</span><span class="special">*&gt;</span> <span class="identifier">cmatch</span><span class="special">;</span>
  140. <span class="keyword">typedef</span> <span class="identifier">match_results</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">wchar_t</span><span class="special">*&gt;</span> <span class="identifier">wcmatch</span><span class="special">;</span>
  141. <span class="keyword">typedef</span> <span class="identifier">match_results</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span><span class="special">&gt;</span> <span class="identifier">smatch</span><span class="special">;</span>
  142. <span class="keyword">typedef</span> <span class="identifier">match_results</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">wstring</span><span class="special">::</span><span class="identifier">const_iterator</span><span class="special">&gt;</span> <span class="identifier">wsmatch</span><span class="special">;</span>
  143. <span class="special">}</span>
  144. </pre>
  145. <p>
  146. The algorithms <a class="link" href="ref/regex_search.html" title="regex_search"><code class="computeroutput"><span class="identifier">regex_search</span></code></a>
  147. and <a class="link" href="ref/regex_match.html" title="regex_match"><code class="computeroutput"><span class="identifier">regex_match</span></code></a>
  148. make use of <a class="link" href="ref/match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a>
  149. to report what matched; the difference between these algorithms is that <a class="link" href="ref/regex_match.html" title="regex_match"><code class="computeroutput"><span class="identifier">regex_match</span></code></a>
  150. will only find matches that consume <span class="emphasis"><em>all</em></span> of the input text,
  151. where as <a class="link" href="ref/regex_search.html" title="regex_search"><code class="computeroutput"><span class="identifier">regex_search</span></code></a>
  152. will search for a match anywhere within the text being matched.
  153. </p>
  154. <p>
  155. Note that these algorithms are not restricted to searching regular C-strings,
  156. any bidirectional iterator type can be searched, allowing for the possibility
  157. of seamlessly searching almost any kind of data.
  158. </p>
  159. <p>
  160. For search and replace operations, in addition to the algorithm <a class="link" href="ref/regex_replace.html" title="regex_replace"><code class="computeroutput"><span class="identifier">regex_replace</span></code></a> that we have already
  161. seen, the <a class="link" href="ref/match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a>
  162. class has a <code class="computeroutput"><span class="identifier">format</span></code> member that
  163. takes the result of a match and a format string, and produces a new string
  164. by merging the two.
  165. </p>
  166. <p>
  167. For iterating through all occurrences of an expression within a text, there
  168. are two iterator types: <a class="link" href="ref/regex_iterator.html" title="regex_iterator"><code class="computeroutput"><span class="identifier">regex_iterator</span></code></a> will enumerate over
  169. the <a class="link" href="ref/match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a>
  170. objects found, while <a class="link" href="ref/regex_token_iterator.html" title="regex_token_iterator"><code class="computeroutput"><span class="identifier">regex_token_iterator</span></code></a> will enumerate
  171. a series of strings (similar to perl style split operations).
  172. </p>
  173. <p>
  174. For those that dislike templates, there is a high level wrapper class <a class="link" href="ref/deprecated/old_regex.html" title="High Level Class RegEx (Deprecated)"><code class="computeroutput"><span class="identifier">RegEx</span></code></a>
  175. that is an encapsulation of the lower level template code - it provides a simplified
  176. interface for those that don't need the full power of the library, and supports
  177. only narrow characters, and the "extended" regular expression syntax.
  178. This class is now deprecated as it does not form part of the regular expressions
  179. C++ standard library proposal.
  180. </p>
  181. <p>
  182. The POSIX API functions: <a class="link" href="ref/posix.html#boost_regex.ref.posix.regcomp"><code class="computeroutput"><span class="identifier">regcomp</span></code></a>, <a class="link" href="ref/posix.html#boost_regex.ref.posix.regexec"><code class="computeroutput"><span class="identifier">regexec</span></code></a>, <a class="link" href="ref/posix.html#boost_regex.ref.posix.regfree"><code class="computeroutput"><span class="identifier">regfree</span></code></a> and [regerr], are available
  183. in both narrow character and Unicode versions, and are provided for those who
  184. need compatibility with these API's.
  185. </p>
  186. <p>
  187. Finally, note that the library now has <a class="link" href="background/locale.html" title="Localization">run-time
  188. localization support</a>, and recognizes the full POSIX regular expression
  189. syntax - including advanced features like multi-character collating elements
  190. and equivalence classes - as well as providing compatibility with other regular
  191. expression libraries including GNU and BSD4 regex packages, PCRE and Perl 5.
  192. </p>
  193. </div>
  194. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  195. <td align="left"></td>
  196. <td align="right"><div class="copyright-footer">Copyright &#169; 1998-2013 John Maddock<p>
  197. Distributed under the Boost Software License, Version 1.0. (See accompanying
  198. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  199. </p>
  200. </div></td>
  201. </tr></table>
  202. <hr>
  203. <div class="spirit-nav">
  204. <a accesskey="p" href="install.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unicode.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  205. </div>
  206. </body>
  207. </html>