Emacs Lisp: Regex Named Character Class and Syntax Table
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.
Syntax table is hard to work with, and regex using it may be unpredictable. 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, that some elisp regex pattern depend on elisp syntax table. ;; save this file as test.el, and open it. ;; M-x emacs-lisp-mode ;; put cursor right after the paren, and M-x eval-last-sexp (re-search-forward "[[:space:]]") ;; it'll move cursor to a space ;; now, set space to have word syntax. Eval this (modify-syntax-entry 32 "w" emacs-lisp-mode-syntax-table) ;; try the search above again. Now it won't find any
After the above, you'll need to restart emacs. Because, now cursor moving commands will consider space as part of word.
Emacs Lisp, Regex in Lisp Code
- Emacs Lisp: Regular Expression
- Emacs Lisp: Regex Functions
- Emacs: Regular Expression Syntax
- Emacs Lisp: Regex Backslash in Lisp Code
- Emacs Lisp: Case Sensitivity (case-fold-search)
- Emacs Lisp: Find Replace Text in Buffer
- Emacs Lisp: Match Data (Regex Result)
- Emacs Lisp: Unicode Escape Sequence
- Emacs Lisp: Convert Regex to Lisp Regex String
- Emacs Lisp: Tip: How to Test Regex
- Emacs Lisp: Regex in Readable Syntax, Package Rx
- Emacs Lisp: Regex Named Character Class and Syntax Table
- Emacs Regex vs Regex in Python, JavaScript, Java