2011-12-15 87 views
2

我喜歡在沒有周圍文本的單獨緩衝區中看到正則表達式的匹配列表。例如,我喜歡看到在我的HTML文檔中定義的所有類名。另外,我希望看到每場比賽的計數。有沒有任何emacs庫?提取正則表達式的匹配?

例如,如果我有一個文本:

 
add related resources or links 
always respect the original author" 

和正則表達式

 
"re." 

我找的比賽和計數

 
rel: 1 
res: 2 

請注意該版本是由「相關「,資源來自」資源「和」尊重「

+1

你可以嘗試給出一個你想要的輸入和輸出的例子嗎?如果你減少閱讀者分析和理解你的問題所需的工作量,他們就更有可能回答。 – 2011-12-15 14:19:20

回答

5
(defun my-re-counter (regexp) 
    (interactive "sregexp: ") 
    (let (matches) 
    (goto-char (point-min)) 
    (while (re-search-forward regexp nil t) 
     (let ((match (assoc (match-string 0) matches))) 
     (if match 
      (setcdr match (1+ (cdr match))) 
      (push (cons (match-string 0) 1) matches)))) 

    (pop-to-buffer "*regexp counts*") 
    (erase-buffer) 
    (dolist (match matches) 
     (insert (car match) ": " (int-to-string (cdr match)) "\n")) 
    (goto-char (point-min)))) 
+0

太棒了,正如我所問。這可以進一步增強。例如,當您點擊結果中的特定匹配項時,它將以M-x在另一個緩衝區中出現M-x突出顯示 - 正則表達式的方式顯示它們。另一種合適的顯示器將與M-x org模式中的樹節點一樣。 – aartist 2011-12-15 16:18:29