dynamic_regexes.qbk 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. [/
  2. / Copyright (c) 2008 Eric Niebler
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section Dynamic Regexes]
  8. [h2 Overview]
  9. Static regexes are dandy, but sometimes you need something a bit more ... dynamic. Imagine you are developing
  10. a text editor with a regex search/replace feature. You need to accept a regular expression from the end user
  11. as input at run-time. There should be a way to parse a string into a regular expression. That's what xpressive's
  12. dynamic regexes are for. They are built from the same core components as their static counterparts, but they
  13. are late-bound so you can specify them at run-time.
  14. [h2 Construction and Assignment]
  15. There are two ways to create a dynamic regex: with the _regex_compile_
  16. function or with the _regex_compiler_ class template. Use _regex_compile_
  17. if you want the default locale. Use _regex_compiler_ if you need to
  18. specify a different locale. In the section on
  19. [link boost_xpressive.user_s_guide.grammars_and_nested_matches regex grammars],
  20. we'll see another use for _regex_compiler_.
  21. Here is an example of using `basic_regex<>::compile()`:
  22. sregex re = sregex::compile( "this|that", regex_constants::icase );
  23. Here is the same example using _regex_compiler_:
  24. sregex_compiler compiler;
  25. sregex re = compiler.compile( "this|that", regex_constants::icase );
  26. _regex_compile_ is implemented in terms of _regex_compiler_.
  27. [h2 Dynamic xpressive Syntax]
  28. Since the dynamic syntax is not constrained by the rules for valid C++ expressions, we are free to use familiar
  29. syntax for dynamic regexes. For this reason, the syntax used by xpressive for dynamic regexes follows the
  30. lead set by John Maddock's [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1429.htm proposal]
  31. to add regular expressions to the Standard Library. It is essentially the syntax standardized by
  32. [@http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf ECMAScript], with minor changes
  33. in support of internationalization.
  34. Since the syntax is documented exhaustively elsewhere, I will simply refer you to the existing standards, rather
  35. than duplicate the specification here.
  36. [h2 Internationalization]
  37. As with static regexes, dynamic regexes support internationalization by allowing you to specify a different
  38. `std::locale`. To do this, you must use _regex_compiler_. The _regex_compiler_ class has an `imbue()` function.
  39. After you have imbued a _regex_compiler_ object with a custom `std::locale`, all regex objects compiled by
  40. that _regex_compiler_ will use that locale. For example:
  41. std::locale my_locale = /* initialize your locale object here */;
  42. sregex_compiler compiler;
  43. compiler.imbue( my_locale );
  44. sregex re = compiler.compile( "\\w+|\\d+" );
  45. This regex will use `my_locale` when evaluating the intrinsic character sets `"\\w"` and `"\\d"`.
  46. [endsect]