gpt4 book ai didi

c++ - 使用 Boost::Xpressive 匹配单个字符

转载 作者:行者123 更新时间:2023-11-28 01:09:06 25 4
gpt4 key购买 nike

我有一个字符串可以是"/""+""."或描述性名称

我正在尝试弄清楚如何使用正则表达式来检查字符串是否与上面的 3 个特殊字符(/+ 或 .)中的任何一个匹配

读了一些书后,我决定采用 boost::xpressive,但我还是想不通。

Boost:xpressive 适合这项任务吗?我的正则表达式字符串需要是什么?

谢谢

最佳答案

为什么不直接使用 std::string::find_first_of() 来做您自己的解决方案?听起来像很多机器来完成一项相当简单的任务。

编辑

如果您仍然卡住,试试这个。

#include <iostream>
#include <boost/xpressive/xpressive.hpp>

using namespace std;
using namespace boost::xpressive;

int main()
{
sregex re = sregex::compile("[+./]|[:word:]+");

sregex op = as_xpr('+') | '.' | '/';
sregex rex = op | (+alpha);

if (regex_match(string("word"), re))
cout << "word" << endl;
if (regex_match(string("word2"), re))
cout << "word2" << endl;
if (regex_match(string("+"), re))
cout << "+" << endl;
return 0;
}

有两种方法可以完成显示的同一件事。名为 re 的变量使用类似 perl 的正则表达式字符串进行初始化。 rex 使用 Xpressive 原生元素。

关于c++ - 使用 Boost::Xpressive 匹配单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4410114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com