Main | UNIX | Emacs

Emacs: Regexps

Last change: Feb 9 2004 AD

Regexp = RegExp = Regular Expression.
^J (or a new line in minibuffer)
matches a newline; to type it press C-q C-j.
*?
non-gready search = match as little as possible; is gready by default.

1. In a Text (M-x search-regexp) 

Ignore newline in a string: match "(text with newlines)" in HTML

RegExp: \W"\(.\|^J\)*?" (\W matches non-word-constituent)
Ex: "txt" => „txt“: replace \(\W\)"\(\(.\|^J\)*?\)" by \1„\2“

2. As a Function 

Define a function to perform the search (&replace). Thus the RegExp becomes a string and any '\' must be doubled: '\\'.

Replace a HTML tag by tag X (accept newlines inside a string)

Search for <TAG(whatever including newline, not gready)</(the same TAG)>

(defun replace-html-tag ()
   (while (re-search-forward "\\(<\\(TAG\\)\\(.\\|\n\\)*?</\\2>\\)" nil t) (replace-match "X\\&X" t nil)))


Jakub Holư 2004 AD