Anton Tarasenko
Anton Tarasenko

Reputation: 8465

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". 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?

Upvotes: 3

Views: 1906

Answers (1)

event_jr
event_jr

Reputation: 17707

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

Also . won't match newlines.

E.g.

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

Upvotes: 4

Related Questions