【发布时间】:2014-05-11 04:48:08
【问题描述】:
为什么这段代码没有将“Mozilla/5.0”替换为“我的用户代理”?
正则表达式在 Notepad++ 中运行良好,但在我的控制台应用程序中不起作用。
#include <regex>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string header =
"GET / HTTP/1.1\r\n\
Host: com.com\r\n\
User-Agent: Mozilla/5.0\r\n\
Connection: keep-alive";
tr1::regex rx("^(User-Agent:).+$", regex_constants::icase);
header = regex_replace(header, rx, "$1 My User Agent", regex_constants::format_first_only);
return 0;
}
【问题讨论】:
-
你用的是什么编译器?直到最近才完全支持正则表达式 C++11。可能是 Notepad++ 使用了与您的控制台不同的编译器(这是在您没有收到编译错误的情况下)
-
我使用 VS 2012。平台工具集:Visual Studio 2012 (v110)
-
用VS编译时是否报错?还是什么都没发生?
-
不,没有错误。但标题没有改变。
-
当您说“在 Notepad++ 中工作正常”时,您的意思是您正在使用 Notepad++ 编译程序并且它可以工作?还是您在 Notepad++ 中使用正则表达式?