I have the following line:
#ifdef DCC_3D_HPP || DCC_2D_HPP
I want to replace it with:
#if defined(DCC_3D_HPP) || defined(DCC_2D_HPP)
I use the following prompt with replace-regexp
:
#ifdef \([A-Z0-9_]+\) \|\| \([A-Z0-9_]+\) → #if defined(\1) || defined(\2)
When I do that, I get the following strange result:
#if defined(DCC_3D_HPP) || defined()|#if defined() || defined()|#if defined() || defined() #if defined() || defined()D#if defined() || defined()C#if defined() || defined()C#if defined() || defined()_#if defined() || defined()2#if defined() || defined()D#if defined() || defined()_#if defined() || defined()H#if defined() || defined()P#if defined() || defined()P#if defined() || defined()
I'm not sure why this happens, nor how to debug it. I have an idea that this has something to do with the fact that consecutive replacements also constitute matches for my expression, and that's probably the culprit.
My question is, how do I get it to do exactly what I want, and is there a way to debug these kinds of expressions to see what goes wrong? For example, replace only once, then see what happened? Thank you.
EDIT: Running query-regex-replace
shows that, for whatever reason, the expression I gave matches the first portion of what I need replaced, i.e. #ifdef DCC_3D_HPP
, and then replaces that only. I have no idea why it does this. It also seems to match empty lines before the necessary lines. I feel like I completely misunderstand something here.
[|][|]
or[|]\{2\}
insteadre-builder
helpful when constructing regexps: doM-x re-builder
in the above buffer (best to have it in a single window in the frame) and type you regexp; it will show you what is matched at every stage.||
C-c TAB string RET
inre-buildier
to select the "replace-regex" style of regexes, otherwise you'll have to translate multiple blackslashes yourself