From: Ingo Wardinski
Subject: regexp replacement
Date: 
Message-ID: <ynly790as5o.fsf@gfz-potsdam.de>
Hello,
I have a newbie question concerning regexp replacement. I would like
to replace regexp like
"ARTICLE{abc1999," with "ARTICLE{Abc:1999," in a file
I started to set up a little lisp routine

(goto-line 1)
(while (re-search-forward "ARTICLE{[a-z]*[0-9]+" nil t)
  (replace-match "" nil nil))
But I'm stucked. How should the replace-match line look like?
Any help would be very appreciated.
TIA
ingo

-- 

From: Thomas F. Burdick
Subject: Re: regexp replacement
Date: 
Message-ID: <f1586e58-64cd-4595-a729-b688942f3c3b@s8g2000prg.googlegroups.com>
I'm cross-posting this to comp.emacs, which is where Emacs Lisp
questions are usually directed.

On Mar 3, 1:49 pm, ····@gfz-potsdam.de (Ingo Wardinski) wrote:
> Hello,
> I have a newbie question concerning regexp replacement. I would like
> to replace regexp like
> "ARTICLE{abc1999," with "ARTICLE{Abc:1999," in a file
> I started to set up a little lisp routine
>
> (goto-line 1)
> (while (re-search-forward "ARTICLE{[a-z]*[0-9]+" nil t)
>   (replace-match "" nil nil))
> But I'm stucked. How should the replace-match line look like?
> Any help would be very appreciated.

You can get the subparts of what matched by creating groups in your
regular expression, and then access them with match-data (group zero
is always the whole expression that matched).  Then you can construct
the string you want to use as a replacement:

  (goto-char (point-min))
  (while (re-search-forward "\\(ARTICLE{\\)\\([a-z]*\\)\\([0-9]+\\)"
nil t)
    (destructuring-bind (start0 end0 start1 end1 start2 end2 start3
end3)
	(match-data)
      (let ((newtext (format "%s%s:%s"
			     (buffer-substring start1 end1)
			     (capitalize (buffer-substring start2 end2))
			     (buffer-substring start3 end3))))
	(replace-match newtext t t))))

Or you could write the code using the editing primitives, so it looks
more like how you'd do it by hand:

  (goto-char (point-min))
  (while (re-search-forward "ARTICLE{\\([a-z]*\\)[0-9]+" nil t)
    (goto-char (match-beginning 1))
    (capitalize-word 1)
    (goto-char (match-end 1))
    (insert ":")
    (goto-char (1+ (match-end 0)))))
From: Ingo Wardinski
Subject: Re: regexp replacement
Date: 
Message-ID: <ynlzltfbxty.fsf@gfz-potsdam.de>
Thomas,
Thank you for both of your suggested solutions. Both work fine.
ingo

-- 
GFZ Potsdam,Sec.2.3, Telegrafenberg, +493312881256,····@gfz-potsdam.de
**********************************************************************
KOLLAPS NICHT VIEL ZEIT/ NICHT VIEL ZEIT/ SCHLAG SCHNELLER/
SCHREI LAUTER/ LEB SCHNELLER/ BIS ZUM KOLLAPS NICHT VIEL ZEIT