我試圖捕獲由字母,數字和下面的%符號組成的下劃線組成的文本的每個實例,只要它不是%%或轉義百分比。我寫了下面的正則表達式來做到這一點:使用D捕獲和存儲正則表達式的所有匹配使用D
((?<!(?:\\|%))%[a-zA-Z0-9_]+)
我想保存它抓住以這種方式爲關聯數組的一切,所以我寫了下面的函數來做到這一點:
string[string] make_symbol_table(string input) {
string[string] symbol_table;
auto m = matchAll(input, regex(r"((?<!(?:\\|%))%[a-zA-Z0-9_]+)", "g")).captures();
for (auto i = 1; i < m.length; i++) {
symbol_table[m[i]] = null;
}
return symbol_table;
}
並測試了以下輸入:
This is an ordinary %template, with a few well-situated %template_arguments. It uses a range of characters, mostly to ensure that %template1 works correctly.\n\nYou can even start %1template with a number! We can also have some silly cases: %_ %1, %a, and so on. %%DIRECTIVES should never be captured, nor should escaped \\% or \\%\\%. %CAPS or %CaPs are fine too.
,我已經寫了作爲一個轉義字符串。我認爲這會給我9場比賽(由this確認),但由於某種原因,我只得到1!我是否正確使用matchAll
?