I am a bit confused on some aspects of Emacs regexps. Specifically with the paragraph on this page

  1. \W’ matches a character which is not within a word
  2. \B’ matches characters which are not a word boundary
  3. \`’ matches the beginning of the whole input
  4. \'’ matches the end of the whole input

Can someone give examples of where each of these might apply?

Also suppose for a generic regexp R, if I augment it as ^R$ what does that mean? Does it mean it matches the expression at the beginning and end of sentences. Elsewhere on this site I have seen the regexp ^\s-$ being used to detect blank lines.

I understand each of ^ and $ in isolation but not when used together

1 Answer 1


^ matches an empty string at a beginning of a line, $ at an end of a line. Therefore, ^R$ matches a line that contains the single character R.

To experiment, you can try

(string-match "\\W" "abc d")

The backslash needs to be doubled in double quotes.

It returns 3, because the space is not within a word (and the first character has index 0).

\B matches inside a word, so for example, the "b" would match in " abc".

I'm not sure how to explain the very beginning and end of the input. string-match will always return 0 for \` and the length of the string for \'. So, if the string contains no newlines, ^ and \` are equivalent, as are $ and \'. If the string contains newlines, ^ matches at each line's start and $ at each line's end, while \` still matches at the very beginning of the string only, and similarly for \' and the very end.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.