c++ - 如何在 C++ 中使用 boost 正则表达式解析转义元素 '\' 和 unicode 字符 '\u'

标签 c++ boost-regex

我正在使用 C++ 中的 boost 正则表达式解析文本文件。我正在从文件中寻找“\”字符。此文件还包含一些 unicode '\u' 字符。那么,有没有办法将'\'和'\u'字符分开。 以下是我解析的test.txt内容

"ID": "\u01FE234DA - this is id ",
"speed": "96\/78",
"avg": "\u01FE234DA avg\83"

以下是我的尝试

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

using namespace std;
const int BUFSIZE = 500;

int main(int argc, char** argv) {

    if (argc < 2) {
        cout << "Pass the input file" << endl;
        exit(0);
    }

   boost::regex re("\\\\+");
   string file(argv[1]);
   char buf[BUFSIZE];

   boost::regex uni("\\\\u+");


   ifstream in(file.c_str());
   while (!in.eof())
   {
      in.getline(buf, BUFSIZE-1);
      if (boost::regex_search(buf, re))
      {
          cout << buf << endl;
          cout << "(\) found" << endl;
          if (boost::regex_search(buf, uni)) {
              cout << buf << endl;
              cout << "unicode found" << endl;

          }

      }

   }
}

现在,当我使用上面的代码时,它打印如下

"ID": "\u01FE234DA - this is id ",
 (\) found
"ID": "\u01FE234DA - this is id ",
 unicode found
"speed": "96\/78",
 (\) found
"avg": "\u01FE234DA avg\83"
 (\) found
 "avg": "\u01FE234DA avg\83"
 unicode found

我不想关注

 "ID": "\u01FE234DA - this is id ",
 unicode found
"speed": "96\/78",
 (\) found
 "avg": "\u01FE234DA avg\83"
 (\) and unicode found

我认为代码无法分别区分“\”和“\u”,但我不确定在哪里更改什么。

最佳答案

尝试在您的第一个正则表达式中使用 [^u] 来匹配任何不是 u 的字符。

boost::regex re("\\\\[^u]");  // matches \ not followed by u
boost::regex uni("\\\\u");  // matches \u

最好使用一个正则表达式。

boost:regex re("\\\\(u)?"); // matches \ with or without u

然后检查部分匹配 m[1] 是否为 'u':

m = boost::regex_search(buf, uni)
if (m && m[1] === "u") {  // pseudo-code
    // unicode
}
else {
    // not unicode
}

最好使用正则表达式进行模式匹配。它们看起来更复杂,但实际上一旦您习惯了它们就更容易维护,并且比一次迭代一个字符的字符串更不容易出错。

关于c++ - 如何在 C++ 中使用 boost 正则表达式解析转义元素 '\' 和 unicode 字符 '\u',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36437735/

相关文章:

c++ - 针对许多离散值进行测试的首选方法?

c++ - 无法调用命名空间方法

c++ - C++中两个 vector 的逐元素乘法

c++ - 这个 C++ 模板参数推导不正确吗?

c++ - 使用 Boost::Regex 从文本文件中提取子字符串

c++ - 使用结构数组取消引用时的段错误

c++ - 帮助 boost::regex 修剪

php - 如何在 C++ boost 中编写这些正则表达式?

c++ - Boost正则表达式如何将Cookie字符串解析为map<string, string>?

c++ - 不知道 "return ch[c-' A']”是怎么回事