emacs - 用于组织模式导出的自定义突出显示的替换正则表达式查询
问题描述
我正在尝试关注这篇博文,并将用户定义的文本标记添加到我的 org 文件中,以便在我的 html 和 latex-pdf 导出中突出显示。
- 博客文章包含一个正则表达式示例(我引用了),但它们的示例(至少对我而言)对于下面详述的任何示例情况都不能正常工作,所以我正在尝试编写自己的示例,但我需要一些帮助.
- (他们在博客中的逻辑适用于使用我的高亮符号(
@
)和乳胶输出(\hl{}
)我相信如下:)
- (他们在博客中的逻辑适用于使用我的高亮符号(
(let ((text (replace-regexp-in-string "[^\\w]\\(@\\)[^\n\t\r]+\\(@\\)[^\\w]" "\\\\hl{" text nil nil 1 nil)))
(replace-regexp-in-string "[^\\w]\\(\\\\hl{\\)[^\n\t\r]+\\(@\\)[^\\w]" "}" text nil nil 2 nil)))
(在 org-mode 中)我将要在@
符号中突出显示的文本括起来,并为乳胶突出显示进行以下转换。
- 第一个@ -> \hl{
- 第二个@-> }
4 个输入的预期输出:
- @highlight me@ -> \hl{highlight me}
- 比尔@高亮我@ -> 比尔\hl{高亮我}
- @highlight me@Bob -> \hl{highlight me}Bob
- @highlight me@ 和 @highlight me@ -> \hl{highlight me} 和 \hl{highlight me}
我的 org-mode 代码块在 4 种情况下测试正则表达式逻辑:
#+begin_src emacs-lisp :tangle yes
; 4 regex cases to convert
(setq mylist '("@highlight me@" "Bill@highlight me@" "@highlight me@Bob" "@highlight me@ and @highlight me@"))
(defun highlight-attempt (text)
"replace @text@ with \hl{text}"
(let ((text (replace-regexp-in-string "[^\\w]\\(@\\)" "}" text nil nil 1 nil)))
(replace-regexp-in-string "\\(^@\\)[^\\w]" "\\\\hl{" text nil nil 1 nil)))
(mapcar 'highlight-attempt mylist)
#+end_src
以上 4 个输入的当前输出:
- @highlight me@ -> \hl{highlight me}
- (如预期)
- 比尔@高亮我@ -> 比尔}高亮我}
- (不像预期的那样)
- @highlight me@Bob -> \hl{highlight me}Bob
- (如预期)
- @highlight me@ 和 @highlight me@ -> \hl{highlight me} 和 }highlight me}
- (不像预期的那样)
解决方案
该博客使用了不正确的正则表达式,请参阅elisp regexps。即[^\\w]
表示任何不是文字\
或w
-\\w
在[...]
. elisp 中的替代方案是\\W
和[^[:word:]]
。我会使用另一种方法,只是将文本保留在外部“@”之间
(replace-regexp-in-string
"@\\([^@]+\\)@"
;; keep the inner text (match is '\\1' in replacement)
"\\\\hl{\\1}"
text)
推荐阅读
- vim - 为什么 SHIFT+V (VISUAL LINE) 不像 CTRL+V (VISUAL BLOCK) 那样插入多行?
- python - 计数输出
- r - 如何使用 usmap 包将状态变量转换为 FIPS 代码?
- api - YouTube 网络请求奇怪的行为
- java - 为什么由于以下 Java 代码,表中的数据会更新为 null?
- angular - 如何测试是否在 jasmin 中触发了 console.error
- dataframe - 如何导出 R 的示例数据集之一
- java - 如何向 CheckMenuItem 添加工具提示?
- sql - 使用 COALESCE 连接时,大于 9 的数字显示为星号 *
- javascript - div结束后停止水平滚动