0
我試圖通過分割和保留分號如分號,括號,點和雙引號來讓我的分詞器工作。C++正則表達式令牌分隔符
string s ("main() a; i, Keyboard.readInt(HOW MANY NUMBERS?);");
regex e ("([.,;-]|[^.,;-]+)");
regex_iterator<std::string::iterator> rit (s.begin(), s.end(), e);
regex_iterator<std::string::iterator> rend;
while (rit!=rend) {
cout << rit->str() << endl;
++rit;
}
當我編譯它時,它有點破了。我的正則表達式([.,;-]|[^.,;-]+)
有什麼問題嗎?輸出我得到這個樣子的:
main() a
;
i
,
Keyboard
.
readInt(HOW MANY NUMBERS?)
;
我希望輸出這樣的:
main
(
)
a
;
i
,
Keyboard
.
readInt
(
HOW MANY NUMBERS?
)
;
謝謝。我想到了。 ([()。,; - ] | [^()。,; - ] +) – lowerbound
如果答案有幫助,SO上的習慣是給+1或「回答」,這樣其他人知道在哪裏看看什麼時候出現類似的情況。 –