如何在 C++ 中解析字符串

发布于 2024-07-13 23:49:23 字数 369 浏览 5 评论 0原文

我想解析字符串,以便检查它们是否具有指定的语法。

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

筱果果 2024-07-20 23:49:23

您可以使用正则表达式来完成此操作。

Z344-R565l t

您的正则表达式应该如下所示。 不确定 C++ 使用什么正则表达式库,但这是确保字符串匹配的通用正则表达式。

Z[0-9]+-R[0-9]+l t

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.

Z[0-9]+-R[0-9]+l t
歌枕肩 2024-07-20 23:49:23

使用 boost::regex

#include <string>
#include <boost/regex.hpp>

bool isMatch(std::string input){
    boost::regex r("Z[0-9]*-R[0-9]*l t");
    return boost::regex_search(input, r);
}

您可以做的另一件事是在文件中提供正则表达式列表,每行一个表达式。 使用文件输入创建 boost::regex 对象向量,并迭代需要验证的每个字符串的模式向量。 这不是很有效,但它会起作用。

Use boost::regex

#include <string>
#include <boost/regex.hpp>

bool isMatch(std::string input){
    boost::regex r("Z[0-9]*-R[0-9]*l t");
    return boost::regex_search(input, r);
}

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.

茶色山野 2024-07-20 23:49:23

如果您只想检查语法,Boost::Regex 就可以了。 如果您想在阅读这样的表达式时实际执行某些操作,我建议您将 Boost::Spirit 与以下内容一起使用:

rule<> testFormula = 
    (ch_p('Z') >> int_p) 
    >> (ch_p('-')>>ch_p('R')>>int_p>>ch_p('l')) 
    >> space_p >> ch_p('t');

我已经隔离了表达式的某些部分,您可能希望将其连接到某些操作(使用 [] 运算符)。

有关详细信息,请参阅文档

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 :

rule<> testFormula = 
    (ch_p('Z') >> int_p) 
    >> (ch_p('-')>>ch_p('R')>>int_p>>ch_p('l')) 
    >> space_p >> ch_p('t');

I have isolated parts of the expression which you might want to connect to some action (using [] operator).

See the documentation for more information

§对你不离不弃 2024-07-20 23:49:23

您可能会在谷歌上搜索“运行时解析器生成”或类似的内容...

lexyacc(或其GNU等效项flex 和 bison)在编译时完成它们的工作,并且可能不够灵活以满足您的需求。 (或者他们可能,你不是很具体)。

You might google "runtime parser generation" or something similar...

lex and yacc (or their GNU equivaents flex and bison) do their work at compile time and may not be flexible enough for your needs. (or they may, you're not very specific).

倾城°AllureLove 2024-07-20 23:49:23

如果您有最新的编译器(VC 2008 SP1等),则无需使用boost,正则表达式是TR1的一部分,您可以将它们与此标头一起使用:#include

示例对于日期(您应该使用双 \ 作为转义字符) :

string dateorder = "12/07/2009";

tr1::regex expr("^([1-2][0-9]|0?[1-9]|30|31)/([1-9]|10|11|12)/(2\\\d{3})$");

if (!regex_match(dateorder.begin(),dateorder.end(),expr))
{
    ...
    break;  
}

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):

string dateorder = "12/07/2009";

tr1::regex expr("^([1-2][0-9]|0?[1-9]|30|31)/([1-9]|10|11|12)/(2\\\d{3})$");

if (!regex_match(dateorder.begin(),dateorder.end(),expr))
{
    ...
    break;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文