开发者

Help with an Emacs Regular Expression

开发者 https://www.devze.com 2022-12-17 00:40 出处:网络
I have statements like this all over my code: LogWrite (String1开发者_如何学Python, String2, L\"=======format string======\",

I have statements like this all over my code:

LogWrite (String1开发者_如何学Python,
          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.


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


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:

  • Syntax of Regular Expressions
  • Backslash in Regular Expressions


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

this should do.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号