1

I have a few header strings in a file that I'd like to replace with expected_status. header appears either as headers or header:. I only want to replace those with the colon with expected status, so: header: --> expected_status:

I am using replace-regexp with the following command:

Replace string (default \(header\):?$ -> expected_status):

However, it is not replacing properly. It is replacing the colon as well with expected_status. Is there a way to let emacs only do the replacement on the identified regexp group?

3 Answers 3

3

Is there a way to let emacs only do the replacement on the identified regexp group?

Sure; but not with replace-regexp.

(while (re-search-forward "\\(header\\):" nil t)
  (replace-match "expected_status" nil nil nil 1))

The 1 at the end is the SUBEXP argument to replace-match, specifying the group/sub-expression to replace from the matched regexp.

So, for example:

(defun my-replace-regexp-group (from to group)
  "In all matches for regexp FROM, replace the content of GROUP with TO."
  (interactive "sFrom: \nsTo: \nnGroup: ")
  (while (re-search-forward from nil t)
    (replace-match to nil nil nil group)))
Sign up to request clarification or add additional context in comments.

Comments

1

Would not be easier to forget about groups and do M-x replace-regexp RET header: RET expected_status: RET

Or maybe I am missing what you are trying to achieve.

And to answer your question in a generic way:

To replace only a regexp group you create three groups:

\(the-stuff-before\)\(the-stuff-you-want-to_replace\)\(the-stuff-after\)

Then you replace that by

\1the-stuff-you-want\3

3 Comments

Yes, but the problem is that header has :?. So I still want to it to replace header without colon, but not headers.
What if before and after can be arbitrary? This seems to complicate the regexp. Isn't there a way to just pick a group and replace that?
If before and after are arbitrary, you only need to replace stuffA bu stuffB. That is trivial. In your case, "before" does not exist, and "after" is the regexp ":?$". And you can replace header(:?)$ that by expected_status\1
0

No - but what you can do it mark the part you want to keep and then include it in the replacement expression. Not sure the exact regex you are trying to match - but appears to be "header" with an optional ":" and replace that with "expected_status".

To keep the optional ":" replace "header(:?)$" with "\1:expected_status"

2 Comments

You mean, "(header):?$" with "\1expected_status"? Why are you grouping the colon? Do you group the part that you do not want to replace? It would make sense to group the one you actually want to replace.
Grouping just allows that part of the expression to be used again elsewhere. The whole of the string that matches the regular expression will be replaced - but if you mark the piece you want to keep, you can put it back in the replacement section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.