QString::contains(QRegExp) 应该返回什么?我可以在正则表达式中使用位置元字符吗?
我正在尝试检测我的输入是 URL 还是纯文件路径。我只是检查字符串中的 http:// 或 www,这对我来说就足够了。
所以,我正在尝试 QString::contains(QRegExp) 并且发现它没有返回我期望的结果。我制作了一个片段来证明它是假的:
#include <QtCore>
#include <iostream>
int main(int argc, char *argv[]){
std::cout << "true: " << true << std::endl;
std::cout << "false: " << false << std::endl;
if (argc > 1)
std::cout << "input: " << (QString(argv[1]).contains(QRegExp("^[(http://)(www)]"))) << std::endl;
return 0;
}
如果第一个参数不以 www 或 http:// 开头,则应打印 0,如果以 www 或 http:// 开头,则应打印 1。但是,这是我的结果:
$ ./test
true: 1
false: 0
$ ./test foobar
true: 1
false: 0
input: 0
$ ./test www.google.com
true: 1
false: 0
input: 0x7fffa68f72df
$ ./test ww_foobar.com
true: 1
false: 0
input: 0x7fff177ba65f
有人知道发生了什么事吗?
I'm trying to detect if my input is a URL or a plain file path. I'm simply checking for http:// or www within the string, and that's enough for me.
So, I'm trying QString::contains(QRegExp) and I'm finding that it doesn't return what I expect it to. I've made a snippet to prove that it's bogus:
#include <QtCore>
#include <iostream>
int main(int argc, char *argv[]){
std::cout << "true: " << true << std::endl;
std::cout << "false: " << false << std::endl;
if (argc > 1)
std::cout << "input: " << (QString(argv[1]).contains(QRegExp("^[(http://)(www)]"))) << std::endl;
return 0;
}
It should print 0 if the first parameter does not begin with www or http://, or 1 if it does. But, this are my results:
$ ./test
true: 1
false: 0
$ ./test foobar
true: 1
false: 0
input: 0
$ ./test www.google.com
true: 1
false: 0
input: 0x7fffa68f72df
$ ./test ww_foobar.com
true: 1
false: 0
input: 0x7fff177ba65f
Does anyone know what is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确定你正在做你想做的事吗?
含义:
匹配字符串的开头,后跟
(htp:/)w
之一,您可能想写的是:
Are you sure you are doing what you want?
Means :
Match the start of the string, followed by one of
(htp:/)w
What you probably wanted to write is :