0
我試圖檢測我的輸入是URL還是純文件路徑。我只是在字符串中檢查http://或www,這對我來說已經足夠了。QString :: contains(QRegExp)返回什麼?我可以使用RegExp中的位置元字符嗎?
所以,我想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;
}
應該打印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
有人知道發生了什麼事嗎?
你是對的,方括號應該只用於字符匹配,因爲字符串匹配我應該使用圓括號內的管道。這可能是錯誤。請更正您的答案(它似乎已被截斷),以便我可以接受它。 – Spidey 2011-12-17 01:36:44