boost.spirit用户手册翻译(36):正则表达式分析器

Regular Expression Parser

正则表达式分析器


Regular expressions are a form of pattern-matching that are often used in text processing. Many users will be familiar with the usage of regular expressions. Initially there were the Unix utilities grep, sed and awk, and the programming language perl, each of which make extensive use of regular expressions. Today the usage of such regular expressions is integrated in many more available systems.

正则表达式是经常在字符处理中使用模式匹配的一种形式。很多用户对正则表达式都很熟悉。最初是Unix的工具grep、sed和awk,还有编程语言perl,这些都大量应用了正则表达式。今天对这样的正则表达式的应用已经集成到了很多系统之中。

During parser construction it is often useful to have the power of regular expressions available. The Regular Expression Parser was introduced, to make the use of regular expressions accessible for Spirit parser construction.

由于正则表达式对分析器的构造大有益处,所以正则表达式分析器出现了,作为连接正则表达式与Spirit分析器的桥梁。

The Regular Expression Parser rxstrlit has a single template type parameter: an iterator type. Internally, rxstrlit holds the Boost Regex object containing the provided regular expression. The rxstrlit attempts to match the current input stream with this regular expression. The template type parameter defaults to char const*. rxstrlit has two constructors. The first accepts a null-terminated character pointer. This constructor may be used to build rxstrlit's from quoted regular expression literals. The second constructor takes in a first/last iterator pair. The function generator version is regex_p.

正则表达式分析器rxstrlit 只有一个模板参数:迭代器类。在内部,rxstrlit 有一个包含了给定的正则表达式的Boost Regex对象。rxstrlit 尝试使用这个正则表达式来匹配当前输入流。模板参数默认值为char const*rxstrlit 有两个构造函数。第一个接受零终结字符串指针。这个构造函数可从引号引起的正则表达式字串构造rxstrlit。第二个构造函数接受first/last迭代器对。它的函数生成器版本为regex_p

Here are some examples:

这里是一些例子:

    rxstrlit<>("Hello[[:space:]]+[W|w]orld")
    regex_p("Hello[[:space:]]+[W|w]orld")

    std::string msg("Hello[[:space:]]+[W|w]orld");
    rxstrlit<>(msg.begin(), msg.end());

The generated parser object acts at the character level, thus an eventually given skip parser is not used during the attempt to match the regular expression (see The Scanner Business).

所生成的分析器对象作用于字符层次,因此实际上给定的忽略分析器在尝试匹配正则表达式时并不工作(见扫描器事务)。

The Regular Expression Parser is implemented by the help of the Boost Regex++ library, so you have to have some limitations in mind.

正则表达式分析器的实现依赖于Boost Regex++ 库,因此你要牢记某些限制。

Boost libraries have to be installed on your computer and the Boost root directory has to be added to your compiler #include<...> search path. You can download the actual version at the Boost web site.

你的电脑里必须安装了Boost库,而Boost的根目录必须被添加到编译器的#include<...>的搜索路径中。你可以在Boost网站下载所需的版本。

The Boost Regex library requires the usage of bi-directional iterators. So you have to ensure this during the usage of the Spirit parser, which contains a Regular Expression Parser.

Boost Regex库要求使用双向迭代器。所以在使用包含正则表达式分析器的Spirit分析器时,这点必须得到保证。

The Boost Regex library is not a header only library, as Spirit is, though it provides the possibility to include all of the sources, if you are using it in one compilation unit only. Define the preprocessor constant BOOST_SPIRIT_NO_REGEX_LIB before including the spirit Regular Expression Parser header, if you want to include all the Boost Regex sources into this compilation unit. If you are using the Regular Expression Parser in more than one compilation unit, you should not define this constant and must link your application against the regex library as described in the related documentation.

Boost Regex库不是一个只有头文件的库(虽然Spirit如此),但它提供了包含所有必须的资源的能力,如果你只在一个编译单元里使用它的话。在包含Spirit正则表达式分析器头文件之前定义预处理常量BOOST_SPIRIT_NO_REGEX_LIB,如果你想把Boost Regex的所有资源包含在一个编译单元里的话。如果你在不止一个的编译单元里使用正则表达式分析器,则不应该定义这个常量,而是依据相关文档连接regex库到你的应用程序中。

See regular_expression.cpp for a compilable example. This is part of the Spirit distribution.

可编译的例子见regular_expression.cpp,这是Spirit发布包的组成部分。



Powered by Zoundry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值