0
我想封閉自定義命名空間中的C++頭只庫的命名空間,以避免與客戶代碼的符號名稱衝突。Renamepacing C++頭與perl - 多行問題
這是我寫的perl腳本。它應該把def命名空間放在abc命名空間中。
#!/usr/bin/perl
while (<>) {
s/namespace\s+def\s*
(# group 1 - braced string with balanced number of lbraces and rbraces
\{
(# group 2 - String without braces or recursion to group 1
[^\{\}]* # some string that does not contain braces
| (?1) # recursion of group1
)*
\}
)
/namespace abc {\nnamespace def \1\n}/gcsx;
print;
}
,這裏是我使用的testfile的:
namespace def {}
namespace def { abc }
namespace def { { } }
namespace def { { abcd } }
namespace def {
abc
}
namespace def { // some comment
do_something();
}
namespace def {
do_something();
while (still_not_crashed()) {
do_even_more();
}
}
然而運行腳本的文件僅產生
namespace abc {
namespace def {}
}
namespace abc {
namespace def { abc }
}
namespace abc {
namespace def { { } }
}
namespace abc {
namespace def { { abcd } }
}
namespace def {
abc
}
namespace def { // some comment
do_something();
}
namespace def {
do_something();
while (still_not_crashed()) {
do_even_more();
}
}
一條線命名工作得很好,但只要應該匹配的模式包含一個換行符,它會停止工作,儘管我在我的正則表達式中添加了s - 修飾符。
我在做什麼錯了?
所以你的C++的問題是如何調試您的Perl腳本? –
我添加了標籤C++,因爲我認爲某些cpp開發人員可能遇到過類似的情況。但它主要是perl問題。 – Richard
您正在逐行進行替換,因此它不適用於多行。 – Toto