How does 'subexpression' work in "replace-match" function in emacs
elisp, emacs
Solution
From the manual:
If subexp is non-nil, that says to replace just subexpression number subexp of the regexp that was matched, not the entire match. For example, after matching ‘foo (ba*r)’, calling replace-match with 1 as subexp means to replace just the text that matched ‘(ba*r)’.
You will need to understand subexpressions of a regular expression first, to understand this feature: By delimiting a part of a regular expression with braces, you can group it and then access those groups. This is very useful if you want to reuse parts of the match in the replacement text when doing a `query-replace-regex`. This is simply the way it works here.
Imagine you want to replace every occurence of `bar(SOMETHING)` with `foo(SOMETHING)`. So you first match with `bar(\([^)]*\))` (this means every character besides `)` is allowed within the parentheses and that our first subexpression is everything that is matched between `\(` and `\)`.
Problem
When reading the description of "replace-match" function in Emacs I can't understand what is the 'subexpression' attribute and how it should be used? For example in `(replace-match (format-time-string writestamp-format (current-time)) t t nil 1)` what does the '1' mean?!