help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regexp: match any character including newline


From: Kai Großjohann
Subject: Re: Regexp: match any character including newline
Date: Wed, 16 Oct 2013 17:31:57 +0200
User-agent: Postbox 3.0.8 (Macintosh/20130427)

Yuri Khan wrote:
> 
> To this end, I want to do a regexp replace of:
> 
> ===
> <tr><td>\(.*?\)</td><td>\(.*?\)</td>
>     <td>\(.*?\)</td></tr>
> ===
> 
> with
> 
> ===
> <expression>\1</expression>
> <return_type>\2</return_type>
> <assertion_note>\3</assertion_note>
> ===

You can use keyboard macros, but you will need a mode that understands
XML.  Let's say you install nxml (it's part of Emacs I think).  Let's
say the content is in a file foo.xml, so that nxml mode is turned on.
Consider that point is before the <tr>.  Now you can use C-M-f to move
it before the <td>.  Now you can use C-M-n to move it after the closing
</td>.  Even if the content of <td>...</td> contains tags!

So you can record a keyboard macro that does the following steps:

- Move after the <tr>
- Insert "<expression>"
- Move to after the </td> with C-M-n
- Insert "</expression>" (using C-c /, say)
- Insert a newline
- Insert "<return_type>"
- Move to after the </td> with C-M-n
- C-c / to insert "</return_type>"
- "<assertion_note>", C-M-n, C-c /
- Use C-M-f to move past the closing </tr>

After all of this, you've got:

<tr><expression><td>foo</td></expression>
<return_type><td>bar</td></return_type>
<assertion_node><td>baz</td></assertion_node></tr>

Now you can do this:  You set the mark with C-space.  You move backward
over the whole thing with C-M-p.  Now the whole <tr>...</tr> is marked.
 Now you can use query-replace to replace <tr>, <td>, </td> and </tr>
with nothing in the highlighted region.  (Need to experiment a bit
whether the region goes away after a query-replace.  If it does, C-x C-x
might be your friend.)

See?  No regex anywhere.  Way cool!  Instead, you're exploiting the
navigation that you get from Emacs modes.

Kai



reply via email to

[Prev in Thread] Current Thread [Next in Thread]