Re: [問題] regex_replace取代非comment內的內容
以下處理 /* */ 這種的註解
#include <iostream>
#include <regex>
using namespace std;
int main() {
string text = "\/*comment 1*\/text1\n\/*comment 2*\/text2\n";
string pattern = "([\\s\\S]*?)((?:\/\\*[\\s\\S]+?\\*\/)|$)";
regex re(pattern);
cout << "pattern: " << pattern << endl;
cout << regex_replace(text,re,"$2");
return 0;
}
程式: https://ideone.com/7eCkXG
參考: http://www.cplusplus.com/reference/regex/regex_replace/
※ 引述《boy770329 (A-So)》之銘言:
: 問個regular expreesion的問題 因為試了很久還是找不到解
: 現在想用regex_replace去取代SQL query內的某個字串,條件是那個字串不在註解內
: 因為SQL的註解寫法有一行的--或#
: 多行的/* ... */
: 然後C++的regex只支援lookahead 處理一行註解還可以,但是多行的就會有問題
: 目前寫的regex 長這樣 : ^(?:(?!\-\-)(?!\#).)*取代的字串regex
: 大致上就是遇到--或是#就assert不處理,這樣可以有效跳過單行註解
: 用類似的邏輯想處理多行註解時就會遇到問題
: ^(?:(?!\-\-)(?!\#)(?!\/\*).)*取代的字串regex
: 因為query可能會長得像SELECT * /*註解*/ from table where...
: 會導致一遇到/*後面的東西也都不繼續處理
: 試了半天還是想不到除了先把/*...*/從input query中去掉的解
: 不知道有沒有版友有類似的經驗可以用C++支援的regex語法處理跳過這種情況做replace
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.221.80.36
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1538994090.A.1CE.html
推
10/08 22:42,
5年前
, 1F
10/08 22:42, 1F
→
10/08 22:43,
5年前
, 2F
10/08 22:43, 2F
推
10/08 22:46,
5年前
, 3F
10/08 22:46, 3F
→
10/08 22:52,
5年前
, 4F
10/08 22:52, 4F
推
10/08 23:23,
5年前
, 5F
10/08 23:23, 5F
推
10/09 17:19,
5年前
, 6F
10/09 17:19, 6F
→
10/09 17:20,
5年前
, 7F
10/09 17:20, 7F
→
10/09 17:20,
5年前
, 8F
10/09 17:20, 8F
推
10/10 22:44,
5年前
, 9F
10/10 22:44, 9F
→
10/10 22:44,
5年前
, 10F
10/10 22:44, 10F
→
10/11 00:08,
5年前
, 11F
10/11 00:08, 11F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):