我正在使用 re2.h 并进行部分匹配。
假设输入字符串是“123.45.34.5:8080”。应返回“123.45.34.5”和“8080”。
假设输入字符串是“123.45.34.5”。“123.45.34.5”和“”也应该返回。如何编写正则表达式?以下代码不起作用。
string portRegex = "[ \r\t]*([0-9a-f]*)[ \r\t]*";
string IPRegex = "([^ \r\t]*)^[^:]*";
string alertRegexStr = IPRegex + portRegex;
m_alertRegex = new RE2(alertRegexStr.c_str());
bool match = RE2::PartialMatch(input_string,*m_alertRegex,
&cip,
&source_port);
谢谢,
更新
现在以下代码有效。
string IPRegex = "([^ \r\t:]*)";
string portRegex = "[ \r\t]*:?[ \r\t]*([0-9a-f]*)[ \r\t]*";
但我有一个问题,为什么 " string IPRegex = "([^ \r\t:]*?)"; " 不起作用?*和有什么区别?和*?