簡體   English   中英

C ++正則表達式子匹配不出現

[英]C++ Regex Sub Matches not appearing

我正在做一個正則表達式來修改函數中的第二和第三參數。 我正在使用大多數Linux發行版附帶的regex.h庫。 我似乎無法讓子匹配出現在下面的代碼中。

碼:

       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));

替換工作完美,這告訴我正則表達式是正確的。 但是,不會顯示子匹配項。 為什么子比賽不顯示?

我認為regex_match需要整個字符串匹配。

Important
Note that the result is true only if the expression matches the whole of 
the input sequence. If you want to search for an expression somewhere 
within the sequence then use regex_search. If you want to match a prefix of 
the character string then use regex_search with the flag match_continuous set. 

所以,也許這會工作

".*?tweenArray.push\\\\(addNewPoint\\\\(posTween,\\\\s*(.+?),\\\\s*(.+?),.*"

 .*? 
 tweenArray . push\(addNewPoint\(posTween, 
 \s* 
 ( .+? )                       # (1)
 , \s* 
 ( .+? )                       # (2)
 ,
 .* 

輸出:

 **  Grp 0 -  ( pos 0 , len 69 ) 
tweenArray.push(addNewPoint(posTween, 1 , 2, .3, scale, brushWidth));  
 **  Grp 1 -  ( pos 38 , len 2 ) 
1   
 **  Grp 2 -  ( pos 42 , len 1 ) 
2  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM