1
我正在制定一個正則表達式來修改函數中的第二個和第三個參數。我正在使用大多數Linux發行版附帶的regex.h庫。我似乎無法讓submatches出現在下面的代碼中。C++正則表達式子匹配不出現
代碼:
string s ("tweenArray.push(addNewPoint(posTween, 1 , 2, .3, scale, brushWidth));");
regex e ("(tweenArray.push\\(addNewPoint\\(posTween,)\s*(.+?),\s*(.+?),"); // matches words beginning by "sub"
smatch m;
regex_match (s, m, e);
cout << "match " << 0 << " (" << m[0] << ")" << endl;
cout << "match " << 1 << " (" << m[1] << ")" << endl;
cout << "match " << 2 << " (" << m[2] << ")" << endl;
cout << "match " << 3 << " (" << m[3] << ")" << endl;
cout << regex_replace (s,e,"$1 0, 0");
輸出:
match 0()
match 1()
match 2()
match 3()
tweenArray.push(addNewPoint(posTween, 0 , 0, .3 scale, brushWidth));
替換的作品完美,它告訴我,正則表達式正確。但是,子匹配沒有被顯示。爲什麼不會顯示子匹配?
你匹配組的匹配'addNewPoint'只匹配前兩個參數參數。你可以嘗試這個正則表達式'(tweenArray \\。push \\(addNewPoint \\(posTween,)\\ s *?((。+?\ s *?),)+ \\ s *?(。+? )+ \\)\\);' – Signus
對不起,我應該更具體一些,我只想捕獲addNewPoint函數的第二個和第三個參數。所以我想引用「1」和「2」。 – shockawave123