XML technologies / Regular expressions / Features and dialects / Maintaining and ⟦U+0003⟧ caring for regular expressions / Search and replace in other languages
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
This section briefly examines some other languages and tools with longstanding regex support.
Awk
In awk, an integrated approach is used. /regex/ applies a regex to the current input line, and “var ~ ...” to other data. This shows where Perl gets its matching notation, whereas its substitution operator comes from sed. The original awk had no substitution operator; newer versions provide the sub(...) function:
sub(/Rechtsschreib/, "Rechtschreib")
This applies the regex ˹Rechtsschreib˼ to the current line and replaces the first match with Rechtschreib. The equivalent in Perl or sed would be s/Rechtsschreib/Rechtschreib/.
To replace all matches, awk uses another function, rather than an option or modifier: gsub(/Standart/, "Standard").
Tcl
Tcl uses a procedural approach that looks confusing if its quoting conventions are unfamiliar. Our spelling correction looks like this in Tcl:
regsub Rechtsschreib $var Rechtschreib newvar
This searches the subject string, the contents of var, with the regex ˹Rechtsschreib˼. The first match, if any, is replaced with Rechtschreib , and the resulting string, changed or not, is stored in newvar , written here without a dollar sign. Tcl’s regsub function expects the regex first, the subject string second, the replacement third, and the variable receiving the result fourth. regsub accepts options; for example, -all replaces all matches rather than just the first.
regsub -all Rechtsschreib $var Rechtschreib newvar
The option -nocase enables case-insensitive searching, like the -i option in egrep or the /i modifier in Perl.
GNU Emacs
GNU Emacs, hereafter simply Emacs, is an extremely powerful text editor with a built-in programming language, elisp (Emacs Lisp). It has a procedural regex interface with many functions. One of the most important is re-search-forward, which accepts an ordinary string and interprets it as a regex. Emacs searches from the current position, stops at a match, or fails if none is found. (re-search-forward is called by the editor’s regexp-search facility.)
As the table Overview of regex dialects in selected programs shows, Emacs’s regex dialect uses many backslashes. For example, ˹\<\([a-z]+\)\([\n●\t]\|<[^>]+>\)+\1\>˼ finds doubled words, much like the regex in Introduction to regular expressions. But it cannot be used directly, because Emacs’s regex engine does not understand \t and \n. In Emacs these are string metacharacters, so the regex must be supplied as a quoted string. The string parser converts them to actual tabs and newlines before passing the result to the regex engine. This string representation has advantages and disadvantages. Since elisp already uses plenty of backslashes, the result often looks like scattered toothpicks. Here is a small function searching for doubled words:
(defun FindNextDbl ()
"Zum nächsten verdoppelten Wort gehen. <...>-Tags werden ignoriert." (interactive)
(re-search-forward "\\<\\([a-z]+\\)\\([\n \t]\\|<[^>]+>\\)+\\1\\>")
)The form (define-key global-map "\C-x\C-d"flFindNextDbl) binds this function to Ctrl-x Ctrl-d, allowing a quick search for the next doubled word while editing.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln