如何在 C++ 中解析字符串
我想解析字符串,以便检查它们是否具有指定的语法。
示例:
Str = Z344-R565l t
这里我的要求是在 Z
之后应该有一个数字,之后是 -
,之后应该有 R
后面跟着一个数字,然后是 l
,然后是空格,最后是 t
。
如果除此之外还有任何其他情况,则应该是一个错误。
我必须像这样解析许多不同类型的语法。 如果为所需的每种语法类型编写一个函数,我会很尴尬。 听说yacc或者lex可以解决这个问题。
任何人都可以解释一下我的问题吗?
I want to parse the strings, so that to check whether they have specified syntax or not.
Example:
Str = Z344-R565l t
Here my requirement is after Z
there should be a number and after that a -
and after that R
should be there followed by a number, followed by l
, followed by a space and then finally t
.
If any thing other than this it should be a error.
I have to parse many different kind of syntax like this. I would be awkward if write a function for each type of syntax required. I heard that yacc or lex can solve this problem.
Can any one please throw some light on my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用正则表达式来完成此操作。
Z344-R565l t
您的正则表达式应该如下所示。 不确定 C++ 使用什么正则表达式库,但这是确保字符串匹配的通用正则表达式。
You do this with a regex.
Z344-R565l t
Your regex should look something like this. Not sure what regex library to use for c++, but this is the general regex to make sure that your string matches.
使用 boost::regex
您可以做的另一件事是在文件中提供正则表达式列表,每行一个表达式。 使用文件输入创建 boost::regex 对象向量,并迭代需要验证的每个字符串的模式向量。 这不是很有效,但它会起作用。
Use boost::regex
The other thing that you could do is supply a list of regex expressions in a file, one expression per line. Create a vector of boost::regex objects using the file input and iterate through the vector of patterns for each string you need to validate. It's not very efficient but it will work.
如果您只想检查语法,Boost::Regex 就可以了。 如果您想在阅读这样的表达式时实际执行某些操作,我建议您将 Boost::Spirit 与以下内容一起使用:
我已经隔离了表达式的某些部分,您可能希望将其连接到某些操作(使用 [] 运算符)。
有关详细信息,请参阅文档
Boost::Regex is fine if you just want to check the syntax. If you want to actually do something when you read such an expression, i suggest you use Boost::Spirit with something like :
I have isolated parts of the expression which you might want to connect to some action (using [] operator).
See the documentation for more information
您可能会在谷歌上搜索“
运行时解析器生成
”或类似的内容...lex
和yacc
(或其GNU等效项flex 和
bison
)在编译时完成它们的工作,并且可能不够灵活以满足您的需求。 (或者他们可能,你不是很具体)。You might google "
runtime parser generation
" or something similar...lex
andyacc
(or their GNU equivaentsflex
andbison
) do their work at compile time and may not be flexible enough for your needs. (or they may, you're not very specific).如果您有最新的编译器(VC 2008 SP1等),则无需使用boost,正则表达式是TR1的一部分,您可以将它们与此标头一起使用:
#include
示例对于日期(您应该使用双
\
作为转义字符) :If you have a recent compiler (VC 2008 SP1, etc.), there's no need to use boost, regex are part of TR1 and you can use them with this header:
#include <regex>
Example for a date (you should use double
\
as escape character):