我怎么能写一个正则表达式来匹配行中的,
而不是""
内的<{p}}?
例如:
`uvm_info("body", $sformatf("Value: a = %d, b = %d, c = %d", a, b, c), UVM_MEDIUM)
希望与其下的^
匹配:
`uvm_info("body", $sformatf("Value: a = %d, b = %d, c = %d", a, b, c), UVM_MEDIUM)
^ ^ ^ ^ ^
答案 0 :(得分:1)
以下函数不使用正则表达式,而是将缓冲区的区域解析为sexps,并返回除字符串中的逗号之外的所有逗号的缓冲区位置列表,如果没有此类逗号则返回nil
(defun find-commas (start end)
(save-excursion
(goto-char start)
(let (matches)
(while (< (point) end)
(cond ((= (char-after) ?,)
(push (point) matches)
(forward-char))
((looking-at "[]\\[{}()]")
(forward-char))
(t
(forward-sexp))))
(nreverse matches))))
它适用于您展示的示例,但可能需要调整其他示例或语言。如果您的示例本身位于缓冲区中,请调用
(find-commas (point-min) (point-max))
返回
(17 60 63 66 70)
答案 1 :(得分:0)
试试这个
"[^"]+"|(,)
捕获组1中的,
答案 2 :(得分:0)
您可以使用font-lock首先为注释和字符串提供信息,然后应用字体锁定关键字。
标准解决方案是使用搜索正则表达式的函数替换正则表达式,并跳过注释和字符串中的任何匹配项。
以下内容来自我的包lisp-extra-font-lock(一个突出显示由let
绑定的变量的包,引用的表达式等。)它搜索引号和反引号,但原理是相同的:
(defun lisp-extra-font-lock-is-in-comment-or-string ()
"Return non-nil if point is in comment or string.
This assumes that Font Lock is active and has fontified comments
and strings."
(let ((props (text-properties-at (point)))
(faces '()))
(while props
(let ((pr (pop props))
(value (pop props)))
(if (eq pr 'face)
(setq faces value))))
(unless (listp faces)
(setq faces (list faces)))
(or (memq 'font-lock-comment-face faces)
(memq 'font-lock-string-face faces)
(memq 'font-lock-doc-face faces))))
(defun lisp-extra-font-lock-match-quote-and-backquote (limit)
"Search for quote and backquote in in code.
Set match data 1 if character matched is backquote."
(let (res)
(while (progn (setq res (re-search-forward "\\(?:\\(`\\)\\|'\\)" limit t))
(and res
(or
(lisp-extra-font-lock-is-in-comment-or-string)
;; Don't match ?' and ?`.
(eq (char-before (match-beginning 0)) ??)))))
res))
font-lock关键字如下:
(;; Quote and backquote.
;;
;; Matcher: Set match-data 1 if backquote.
lisp-extra-font-lock-match-quote-and-backquote
(1 lisp-extra-font-lock-backquote-face nil t)
;; ...)