我有一個代碼示例:爲什麼這個正則表達式不匹配這個字符串?
#include <regex>
#include <iostream>
using namespace std;
int main()
{
string input;
regex third("([a-zA-Z]*) ([a-zA-Z]*)[\s]*([a-zA-Z]*)");
smatch third_match;
getline(cin, input);
while (input != "q")
{
if(regex_match(input, third_match, third))
cout << "Ok" << endl;
getline(cin, input);
}
return 0;
}
如果我輸入一個字符串,說:
「我am_____________happy」(空格,而不是下劃線(很多 '_')
。然後,它應該工作 - 因爲我有一個「單詞」,然後是一個「空間」,然後是一個「單詞」,然後「我想要多少空間,然後是一個」單詞「,這應該符合我上面的表達,但是 爲什麼?
是您的輸入函數捕獲行尾字符? –
你的I/O代碼不好。它通常會進入無限循環,因爲它無法檢查I/O操作的結果。 –