Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Boost::Xpressive匹配单个字符_C++_Regex_Boost_Boost Xpressive - Fatal编程技术网

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

C++ 使用Boost::Xpressive匹配单个字符,c++,regex,boost,boost-xpressive,C++,Regex,Boost,Boost Xpressive,我有一个字符串,可以是“/”+“.”或描述性名称 我试图弄明白如何使用正则表达式来检查字符串是否与上面3个特殊字符(/+或)中的任何一个匹配 在读了一点书之后,我决定使用boost::xpressive,但我仍然无法理解它 Boost:xpressive适合此任务吗?我的正则表达式字符串需要是什么 谢谢为什么不使用std::string::find_first_of()来完成您自己的解决方案呢?听起来像是一个相当简单的任务需要很多机器 编辑 如果你仍然被卡住,试试这个 #include <

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

我试图弄明白如何使用正则表达式来检查字符串是否与上面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;
}
#包括
#包括
使用名称空间std;
使用名称空间boost::xpressive;
int main()
{
sregex re=sregex::compile(“[+./]|[:word:][+”;
sregex op=as_xpr('+')|'.|'/';
sregex-rex=op |(+alpha);
if(regex_匹配(字符串(“单词”),re))

cout为什么不直接使用
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;
}
#包括
#包括
使用名称空间std;
使用名称空间boost::xpressive;
int main()
{
sregex re=sregex::compile(“[+./]|[:word:][+”;
sregex op=as_xpr('+')|'.|'/';
sregex-rex=op |(+alpha);
if(regex_匹配(字符串(“单词”),re))

cout我想说Boost.Xpressive对于这项任务来说可能有些过分,但这是你的决定


当您想要验证特定格式的字符串时,正则表达式是救命稻草。这里不涉及任何格式,只涉及一组可能的值。我的建议是:如果您的问题可以通过简单、连续的字符串相等性比较来解决,那么您可能不需要任何类似正则表达式的东西。

我认为这是一个提升。Xpressive对于这项任务来说可能有些过分,但这是你的决定


当您想要验证特定格式的字符串时,正则表达式是救命稻草。这里不涉及格式,只涉及一组可能的值。我的建议是:如果您的问题可以通过简单、连续的字符串相等性比较来解决,那么您可能不需要任何类似正则表达式的东西。

我正在尝试了解regex和我无法找到上述问题的解决方案。我想是相邻的转义字符让我感到困惑。@TomFLuff:那么,那就不同了。正则表达式是非常强大的工具,所以请继续深入研究它们。请记住,不要让解决方案过于复杂而使用一些很酷的东西(我必须经常提醒自己这一点)。我正在努力学习正则表达式,但我想不出上述问题的解决方案。我想是相邻的转义字符让我感到困惑。@TomFLuff:那么,那就不同了。正则表达式是非常强大的工具,所以请继续深入研究它们。请记住,不要让解决方案过于复杂而使用它很酷(我经常提醒自己这一点)。