Emacs正则表达式中的字符串的开头和结尾 [英] Beginning and end of the string in Emacs regexps

查看:176
本文介绍了Emacs正则表达式中的字符串的开头和结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个修剪函数:

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))

但是使用first / nnext(从 shell获取) -command-to-string )它只返回第一个。参考手册说:

But with a string like "first/nnext" (got from shell-command-to-string) it returns only the "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

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

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. To reliably match a non-printing or printing character use [:graph:]. See char class

另外不会匹配换行符。

例如$($)

E.g.

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

这篇关于Emacs正则表达式中的字符串的开头和结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆