[英]Beginning and end of the string in Emacs regexps
What is the characters that indicate the beginning and the end of the string with newlines in it? 哪些字符表示字符串的开头和结尾有换行符? I'm writing a trim function: 我正在写一个修剪函数:
(defun trim (str)
(if (string-match "^[[:space:]]*\\(.+?\\)[[:space:]]*$" str)
(match-string 1 str)
str))
But with a string like "first/nnext" (got from shell-command-to-string
) it returns only the "first". 但是使用像“first / nnext”这样的字符串(从shell-command-to-string
)它只返回“first”。 Reference manual says: 参考手册说:
When matching a string instead of a buffer, '^' matches at the beginning of the string or after a newline character. 匹配字符串而不是缓冲区时,'^'在字符串的开头或换行符后匹配。
\\\\'
and the left one are for beginning/end of a buffer, so it simply returns nothing from a string. \\\\'
和左边的是缓冲区的开头/结尾,所以它只是从字符串中返回任何内容。 Therefore, how to indicate the 'absolute' beginning of a string, if possible? 因此,如果可能的话,如何指示字符串的“绝对”开头?
It's \\\\`
for beginning of buffer or string. 它是\\\\`
用于缓冲区或字符串的开头。 And \\\\'
for end. 并且\\\\'
为了结束。 See manual 见手册
However, I think the root of your confustion isn't the anchor. 但是,我认为你的困难的根源不是锚。 The [:space:]
char class matches different characters based on the current syntax table. [:space:]
char类根据当前语法表匹配不同的字符。 To reliably match a non-printing or printing character use [:graph:]
. 要可靠地匹配非打印或打印字符,请使用[:graph:]
。 See char class 见char类
Also .
还有.
won't match newlines. 不符合换行符。
Eg 例如
(let ((str " \n a\nbc \n "))
(string-match "\\`[^[:graph:]]*\\(\\(?:.\\|\n\\)+?\\)[^[:graph:]]*\\'" str)
(match-string 1 str))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.