dynamic_regexes.qbk

来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 65 行

QBK
65
字号
[/ / Copyright (c) 2008 Eric Niebler / / Distributed under the Boost Software License, Version 1.0. (See accompanying / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) /][section Dynamic Regexes][h2 Overview]Static regexes are dandy, but sometimes you need something a bit more ... dynamic. Imagine you are developinga text editor with a regex search/replace feature. You need to accept a regular expression from the end useras input at run-time. There should be a way to parse a string into a regular expression. That's what xpressive'sdynamic regexes are for. They are built from the same core components as their static counterparts, but theyare late-bound so you can specify them at run-time.[h2 Construction and Assignment]There are two ways to create a dynamic regex: with the _regex_compile_function or with the _regex_compiler_ class template. Use _regex_compile_if you want the default locale. Use _regex_compiler_ if you need tospecify a different locale. In the section on[link boost_xpressive.user_s_guide.grammars_and_nested_matches regex grammars],we'll see another use for _regex_compiler_.Here is an example of using `basic_regex<>::compile()`:    sregex re = sregex::compile( "this|that", regex_constants::icase );Here is the same example using _regex_compiler_:    sregex_compiler compiler;    sregex re = compiler.compile( "this|that", regex_constants::icase );_regex_compile_ is implemented in terms of _regex_compiler_.[h2 Dynamic xpressive Syntax]Since the dynamic syntax is not constrained by the rules for valid C++ expressions, we are free to use familiarsyntax for dynamic regexes. For this reason, the syntax used by xpressive for dynamic regexes follows thelead set by John Maddock's [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1429.htm proposal]to add regular expressions to the Standard Library. It is essentially the syntax standardized by[@http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf ECMAScript], with minor changesin support of internationalization.Since the syntax is documented exhaustively elsewhere, I will simply refer you to the existing standards, ratherthan duplicate the specification here.[h2 Internationalization]As with static regexes, dynamic regexes support internationalization by allowing you to specify a different`std::locale`. To do this, you must use _regex_compiler_. The _regex_compiler_ class has an `imbue()` function.After you have imbued a _regex_compiler_ object with a custom `std::locale`, all regex objects compiled bythat _regex_compiler_ will use that locale. For example:    std::locale my_locale = /* initialize your locale object here */;    sregex_compiler compiler;    compiler.imbue( my_locale );    sregex re = compiler.compile( "\\w+|\\d+" );This regex will use `my_locale` when evaluating the intrinsic character sets `"\\w"` and `"\\d"`.[endsect]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?