4

Emacs 26.1 In regexp help https://www.emacswiki.org/emacs/RegularExpression

has the next regexp:

[:space:] a whitespace character

Is this a same as:

[ \t\r\n\v\f]

and same as

 \s-

?

  • 1
    A good way to experiment with regexps is to use re-builder: it allows you to build a regexp interactively showing you what matches at each step of the way. Call it from a buffer that contains all of the things that you want to test (spaces, newlines, tabs, form-feeds etc). – NickD Nov 17 '19 at 13:55
  • 2
    @NickD it is a good way to experiment. However he's asking whether the regexp is equivalent. With re-builder you can't say with 100% certainty the regexps are equivalent; at most you can say they both match the example text you've tried. – Aquaactress Nov 17 '19 at 17:19
  • That's true: it is not going to provide a definitive answer. Only the documentation (and the sources) will provide that. – NickD Nov 17 '19 at 18:39
5

I recommend reading the Emacs Lisp manual instead of Emacswiki.

Here's some of it on backslash sequences in regular expressions:

‘\sCODE’
     matches any character whose syntax is CODE.  Here CODE is a
     character that represents a syntax code: thus, ‘w’ for word
     constituent, ‘-’ for whitespace, ‘(’ for open parenthesis, etc.  To
     represent whitespace syntax, use either ‘-’ or a space character.
     *Note Syntax Class Table::, for a list of syntax codes and the
     characters that stand for them.

The corresponding part on character classes:

‘[:space:]’
     This matches any character that has whitespace syntax (*note Syntax
     Class Table::).

So, yes, they are the same, both are about characters with whitespace syntax. The exact meaning of whitespace syntax depends on the currently active syntax table, it can be customized by major modes and less commonly, by users. For example if someone defined the comma character to have whitespace syntax in Clojure buffers, then both \s- and [[:space:]] would be matched by a comma.

To summarize, if you want to write a regex matching specific whitespace characters, then there's no other way to get there than actually spelling them out. In case you find it tedious to do so with strings, I highly recommend the built-in rx library as alternative notation that's easier to generate and compose.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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