Elisp: Regex Named Character Class and Syntax Table

By Xah Lee. Date: . Last updated: .

In emacs Regular Expression Syntax, the meaning of some named character class such as [[:word:]] is dependent on the current major mode's Syntax Table.

this means, [[:word:]] may have different meaning in different Major Mode

Best is just to put the chars you want explicitly in your regex, for example, [A-Za-z0-9]+.

Use [[:word:]]+ if you need to match letters of western languages and also Chinese characters or Russian characters etc.

Here's a demo, that some regex depend on syntax table.

here's a demo, to show that some emacs regex pattern depend on syntax table.

copy and paste this text into a emacs buffer.

now set to text-mode by
M-x text-mode

s------------------------------

put cursor right after the right parenthesis, and M-x eval-last-sexp

(re-search-forward "[[:word:]]")

·
name: MIDDLE DOT
codepoint 183

it'll move cursor to the middle dot

now set the buffer to fundamental-mode by
M-x fundamental-mode

repeat the above steps.

it'll move cursor to a letter instead.

emacs and elisp regex