I'm looking for a solution to match two consecutive identical words except for the case of the initial characters with query-replace-regexp
:
The the
the The
I can't figure out how to match these typos with a regular expression at once because I don't know how to use downcase
or capitalize
in the searching part as I usually do in the replacement part (e.g. with lambda
). I mean something like this:
;; THIS CODE IS WRONG!!!
(query-replace-regexp (concat "\\(\\(\\<[A-Z][[:alpha:]]+\\>\\)\\)\\([~\s\n]+\\)"
`((lambda (data count))
(downcase (match-string 1))))
nil (point-min) (point-max))
Of course I know this is wrong and so I tried this code for the first kind of typo:
;; "The the" case
(let (cap-word space typo)
(goto-char (point-min))
(while (re-search-forward "\\(\\([A-Z]\\)\\([[:alpha:]]+\\)\\)\\([ ~\n]+\\)" nil t)
(save-excursion
(setq cap-word (match-string 1)
space (match-string 2)
typo (concat cap-word
space
(downcase cap-word))
(when (looking-at typo)
;; do things
))))
It works, but it's slow and, overall, does not match my typos at once. Is there a way to do that?
Review
I said the code above works, but it's not true. Sorry: I posted the wrong version of the code, so I'm putting the right one below for completeness:
;; "The the" case
(let ((case-fold-search nil)
cap-word space down-word typo)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "\\<\\([A-Z]\\)\\([[:alpha:]]+\\)\\>\\([ ~\n]+\\)" nil t)
(save-excursion
(setq cap-word (concat (match-string 1)
(match-string 2))
space (match-string 3)
down-word (downcase cap-word)
typo (concat cap-word
space
down-word))
(when (looking-at down-word)
;; do things
)))))