左边最小的匹配表达式?

时间:2012-09-06 16:49:55

标签: regex math emacs latex libreoffice

这就是我所拥有的:

{a} {b} over {c} {d}
{a} {{b} over {c}} {d}

(我有一些文字,它有这样的表达)我想将两者都转换为:

{a} \frac{b}{c} {d}

这是我最好的选择:

(goto-char (point-min))
(while (search-forward-regexp "{+?\\(.+?\\)} +over +{\\(.+?\\)}+?" nil t) 
  (replace-match (concat "\\\\" "frac{\\1}{\\2}") t nil))

但它不起作用。我明白了:

\frac{a} {b}{c} {d}
\frac{a} {{b}{c}} {d}

分别

问题是 - 我的正则表达式尽可能在左侧捕获。虽然我需要它尽可能少地捕获。

修改

接受的答案不适用于更复杂的案件(我也需要):

{d^{2} q} over {d t^{2} }
{{d^{2} q} over {d t^{2} }}

(因为嵌套)

这是解析器的工作版本(代码的有趣部分从while开始):

(defun over2frac ()
  (interactive)
  (let (start end
        (case-fold-search nil)
        lStart lEnd lStr
        rStart rEnd rStr)
    (if (use-region-p)
        (progn (setq start (region-beginning)) ;; then
               (setq end (region-end)))
      (progn (setq start (line-beginning-position)) ;; else
             (setq end (line-end-position))))
    (save-restriction 
      (narrow-to-region start end)
      (goto-char (point-min))

      (while (search-forward-regexp "{\\(.+\\)}\s*over\s*{\\(.+?\\)}" nil t) 

        ;; r:
        (goto-char (match-beginning 2))
        (backward-char 1)
        (setq rStart (point))
        (forward-sexp)
        (setq rEnd (point))
        (setq rStr (buffer-substring-no-properties rStart rEnd))

        ;; l:
        (goto-char (match-end 1))
        (forward-char)
        (setq lEnd (point))
        (backward-sexp)
        (setq lStart (point))
        (setq lStr (buffer-substring-no-properties lStart lEnd))

        (delete-region lStart rEnd)
        (insert "\\" "frac" lStr rStr)
        ))))

由于它使用forward-sexpbackward-sexp - 并且未定义syntax-table - 它仅适用于适当的模式(例如rst-mode和{{1} } - 但不是text-mode。对于上面的复杂例子,它给出了:

emacs-lisp-mode

编辑2

\frac{d^{2} q}{d t^{2} }
{\frac{d^{2} q}{d t^{2} }}

这会转换两件事:

(defun backslash-func--args-on-both-sides (Find Replace)
  (goto-char (point-min))
  (while (search-forward-regexp (concat "\\([^{]+\\)}\s*" Find "\s*{\\([^}]+\\)") nil t) 

    ;; r:
    (goto-char (match-beginning 2))
    (backward-char 1)
    (setq rStart (point))
    (forward-sexp)
    (setq rEnd (point))
    (setq rStr (buffer-substring-no-properties rStart rEnd))

    ;; l:
    (goto-char (match-end 1))
    (forward-char)
    (setq lEnd (point))
    (backward-sexp)
    (setq lStart (point))
    (setq lStr (buffer-substring-no-properties lStart lEnd))

    ;; s:
    (goto-char lStart)
    (if (looking-back "{") 
        (setq lStart (1- (point))))

    (goto-char rEnd)
    (if (looking-at "}")
        (setq rEnd (1+ (point))))

    (delete-region lStart rEnd)
    (insert "\\" Replace lStr rStr))

  (goto-char (point-min))
  (while (search-forward-regexp (concat "\\([0-9a-zA-Z_^]+\\)\s*" Find "\s*\\([0-9a-zA-Z_^]+\\)") nil t) (replace-match (concat "\\\\" Replace "{\\1}{\\2}") t nil))
  )

{d^{2} q} over {d t^{2} }
{{d^{2} q} over {d t^{2} }}

我不在这里使用\frac{d^{2} q}{d t^{2} } ,因为所有的变量都是本地的 - 并且defun本身在另一个defun中。 Full code

2 个答案:

答案 0 :(得分:3)

使用此表达式:http://regex101.com/r/kY8jY5

/{+([^{}]+)}\s*over\s*{([^{}]+)}+/g

该表达式应与emacs兼容。

替换为:

\frac{\1}{\2}

注意:我不确定您是否在emacs中使用\1$1

答案 1 :(得分:0)

在正则表达式的开头添加一个贪婪的*,并将其放在替换中:

(goto-char (point-min))
(while (search-forward-regexp "\\(.*\\){+?\\(.+?\\)} +over +{\\(.+?\\)}+?" nil t) 
  (replace-match (concat "\\1\\\\" "frac{\\2}{\\3}") t nil))

我得到它来产生这个:

{a} \frac{b}{c} {d}
{a} {\frac{b}{c}} {d}

我不确定是否有办法摆脱额外的周围{} ......