繁体   English   中英

Emacs regexp中字符串的开头和结尾

[英]Beginning and end of the string in Emacs regexps

哪些字符表示字符串的开头和结尾有换行符? 我正在写一个修剪函数:

(defun trim (str)
  (if (string-match "^[[:space:]]*\\(.+?\\)[[:space:]]*$" str)
      (match-string 1 str)
      str))

但是使用像“first / nnext”这样的字符串(从shell-command-to-string )它只返回“first”。 参考手册说:

匹配字符串而不是缓冲区时,'^'在字符串的开头或换行符后匹配。

\\\\'和左边的是缓冲区的开头/结尾,所以它只是从字符串中返回任何内容。 因此,如果可能的话,如何指示字符串的“绝对”开头?

它是\\\\`用于缓冲区或字符串的开头。 并且\\\\'为了结束。 手册

但是,我认为你的困难的根源不是锚。 [:space:] char类根据当前语法表匹配不同的字符。 要可靠地匹配非打印或打印字符,请使用[:graph:] char类

还有. 不符合换行符。

例如

(let ((str " \n a\nbc \n "))
  (string-match "\\`[^[:graph:]]*\\(\\(?:.\\|\n\\)+?\\)[^[:graph:]]*\\'" str)
  (match-string 1 str))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM