tags:

views:

105

answers:

3

Hi SO,

I have statements like this all over my code:

LogWrite (String1,
          String2,
          L"=======format string======",
          ...
          );

I want to change each of these to:

LogWrite (String1,
          String2,
          L"format string",
          ...
          );

I'm trying to write the regexp required to do this using the Emacs function query-replace-regexp, but not much success yet. Help please!


UPDATE: 1) In case it is not clear, this question is emacs specific.

2) I would like to match the entire code chunk starting from Log... ending at );

3) I used the following reg-exp to match the code chunk:

L.*\n.*\n.*==.*;

I used re-builder to match this regexp. the \n is used because I found that otherwise emacs would stop matching at the new line. The problem is that I don't know how to select the format string and save it to use it in the replace regexp - hence the ==.* part in the regexp. That needs to be modified to save the format string.

A: 
/={7}(.*)={6}/\1/

this should do.

SilentGhost
why the downvote?
SilentGhost
It's not by me, but I think it might be because GNU EREs don't support lazy quantifiers.
Tim Pietzcker
@Tim: surely, one who knows that would see that lazy quantifier is not really necessary. my answer is also exactly the same as Amarghosh's, so I don't really think it's a **real** reason.
SilentGhost
You're right. Let me rectify this injustice :) +1
Tim Pietzcker
thanks, @Tim. But I would still want to hear from those two downvoters.
SilentGhost
+1 to take you back to zero. I've a feeling that I'd be accused of down-voting to gain the edge if I keep silent.
Amarghosh
On a second thought, may be the down voter didn't understand the `/pattern/replace/` syntax.
Amarghosh
for one, /pattern/replace/ syntax isn't an Emacs syntax, and this is specifically an Emacs question; I'm not a down-voter, however.
Michael Paulukonis
@OtherMichael: well OP hasn't posted neither emacs commands nor the regex that he tried (if he did, I'd just correct the regex). I'd imagine the ability to interpret widely used syntax would be in place. I see no need to use emacs here, `sed` command, would be more appropriate and straightforward.
SilentGhost
@silentghost I'm sorry if the question was not clear about specifically using emacs regexp. It's not about getting the job done in this particular case, it's about wanting to learn something so that I can re-apply it elsewhere without having to ask on SO. My problem is that I don't know how to save text to use it in the replace expression since emacs doesn't seem to treat ( as a special character.
vedang
@vedang: i think your real problem is that you're using the wrong tool for the job. If it takes you so long to figure out the simplest of the replacement, may be you should consider finding a more appropriate tool.
SilentGhost
+2  A: 

If you don't have multiple (or escaped) double quotes in those format string lines, you can

//replace

L"=+(.*)=+"

//with 

L"\1"

Update: Removed the lazy quantifier (thanks @tim). Make sure that the regex is not multiline; the greedy * will lead to pretty bad results if . matches new lines

Amarghosh
Emacs doesn't support lazy quantifiers, according to regular-expressions.info
Tim Pietzcker
didn't work. I used re-builder to check your expression, but emacs considers ( as a normal character, not a special character. I tried escaping it with a \ but no luck
vedang
+1  A: 

A great tool to figure out emacs regular expressions is:

M-x re-builder

A brief description from the documentation:

When called up re-builder' attaches itself to the current buffer which becomes its target buffer, where all the matching is done. The active window is split so you have a view on the data while authoring the RE. If the edited expression is valid the matches in the target buffer are marked automatically with colored overlays (for non-color displays see below) giving you feedback over the extents of the matched (sub) expressions. The (non-)validity is shown only in the modeline without throwing the errors at you. If you want to know the reason why RE Builder considers it as invalid call reb-force-update' ("\C-c\C-u") which should reveal the error.

It comes built into Emacs (since 21)

And for the syntax of Emacs regular expressions, you can read these info pages:

Trey Jackson
Thanks for the tip, but I know of re-builder. I simply don't know enough emacs regexp to write the proper expression in this case.
vedang