我想使用正则表达式来解析从套接字接收的数据 .
我编写了一个自定义套接字迭代器,所以我可以将数据传递给_32701的正则表达式函数 .
请记住,数据理论上可能永远不会结束,在发送完整请求后套接字不会关闭,因为客户端需要响应和可能的未来通信 .
让我们假设我们有一个非常简单的协议,请求由 START
或 STOP
组成 .
真正的协议当然要复杂得多,但为了举例,这样做 .
// A simple regular expression to parse this could be defined like so:
static const std::regex re("^(START|STOP)");
// And parsed using:
std::regex_match(begin, end, result, re); // 1
// or using regex_search
std::regex_search(begin, end, result, re); // 2
假设客户端发送单词 START
,等待5秒,然后发送另一个字符,例如 X
. 在这种情况下,方法#1将在返回false之前等待5秒 . 现在假装客户端在原始 START
消息之后没有发送任何内容,方法#1将永远不会返回 .
对于方法#2:假设您的输入是 XSTART
,解析器似乎不理解永远不会找到有效匹配,因为正则表达式以 ^
开头,并且因为输入是无限的,所以它也永远不会终止 .
因此,最后方法#1正确识别无效请求,而方法#2正确识别有效请求,但方法#1在有效请求时陷入无限循环,方法#2卡在无效请求上 .
这个Minimal, Complete, and Verifiable example演示了这个问题:
#include <stdio.h>
#include <stdint.h>
#include <iterator>
#include <vector>
#include <regex>
// stdin iterator that goes against all good
// programming practices for the sake of simplicity
class stdin_iter : public std::iterator<std::bidirectional_iterator_tag, char> {
static std::vector<char> buf;
size_t i;
public:
stdin_iter() : i(SIZE_MAX) {}
stdin_iter(size_t i) : i(i) {}
bool operator==(const stdin_iter& o) const { return i == o.i; }
bool operator!=(const stdin_iter& o) const { return i != o.i; }
value_type operator*() const {
while (i >= buf.size()) buf.push_back(getc(stdin));
return buf[i];
}
stdin_iter& operator++() { i++; return *this; }
stdin_iter operator++(int) { stdin_iter r = *this; i++; return r; }
stdin_iter& operator--() { i--; return *this; }
stdin_iter operator--(int) { stdin_iter r = *this; i--; return r; }
};
std::vector<char> stdin_iter::buf;
int main() {
stdin_iter begin(0), end;
std::regex re("^(START|STOP)");
std::match_results<stdin_iter> result;
//bool valid = std::regex_match(begin, end, result, re); // stuck on valid input
//bool valid = std::regex_search(begin, end, result, re); // stuck on invalid input
bool valid = std::regex_search(begin, end, result, re, std::regex_constants::match_continuous); // mostly works
if (valid) printf("valid: %s\n", result[1].str().c_str());
else printf("invalid\n");
}
一种解决方案是在例如第二次不活动之后向数据添加人工结束 . 但这大大增加了响应时间,感觉不对 .
另一个解决方案是编写一个自定义的正则表达式解析器,但重新发明问题这个问题似乎有点过分 .
有没有更好的方法来使这项工作?
1 回答
使用
std::regex_constants::match_continuous
标志,Luke .