Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regexp/emacs selective replace

1 view
Skip to first unread message

m121212

unread,
Nov 23, 2009, 8:41:34 AM11/23/09
to Help-gn...@gnu.org

Hi,

I have a tricky problem that I'm not sure how to solve. I have a latex
document with several figure environments that look like this:

\begin{figure}
\includegraphics{blah.ps}
\end{figure}

but need to look like this:

\begin{figure}
\begin{center} \includegraphics{blah.ps} \end{center}
\end{figure}

Some of the figure environments already have this however, and I don't want
to end up with something like this:

\begin{figure}
\begin{center}\begin{center} \includegraphics{blah.ps}
\end{center}\end{center}
\end{figure}

Any ideas? Also, is there a way Emacs can just wrap any selection with
custom, predefined tags?

thanks,
m121212
--
View this message in context: http://old.nabble.com/regexp-emacs-selective-replace-tp26478209p26478209.html
Sent from the Emacs - Help mailing list archive at Nabble.com.

Barry Margolin

unread,
Nov 24, 2009, 8:30:25 PM11/24/09
to
In article <mailman.11424.12591020...@gnu.org>,
m121212 <m12...@mailinator.com> wrote:

> Hi,
>
> I have a tricky problem that I'm not sure how to solve. I have a latex
> document with several figure environments that look like this:
>
> \begin{figure}
> \includegraphics{blah.ps}
> \end{figure}
>
> but need to look like this:
>
> \begin{figure}
> \begin{center} \includegraphics{blah.ps} \end{center}
> \end{figure}
>
> Some of the figure environments already have this however, and I don't want
> to end up with something like this:
>
> \begin{figure}
> \begin{center}\begin{center} \includegraphics{blah.ps}
> \end{center}\end{center}
> \end{figure}
>
> Any ideas?

Does query-replace-regexp do what you want? It will prompt you before
each possible replacement, and you can say whether to replace that
instance.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

harven

unread,
Nov 25, 2009, 3:15:27 AM11/25/09
to
m121212 <m12...@mailinator.com> writes:

> Hi,
>
> I have a tricky problem that I'm not sure how to solve. I have a latex
> document with several figure environments that look like this:
>
> \begin{figure}
> \includegraphics{blah.ps}
> \end{figure}
>
> but need to look like this:
>
> \begin{figure}
> \begin{center} \includegraphics{blah.ps} \end{center}
> \end{figure}
>
> Some of the figure environments already have this however, and I don't want
> to end up with something like this:
>
> \begin{figure}
> \begin{center}\begin{center} \includegraphics{blah.ps}
> \end{center}\end{center}
> \end{figure}
>
> Any ideas? Also, is there a way Emacs can just wrap any selection with
> custom, predefined tags?

Anchor to the beginning and end of line.

M-x replace-regexp RET
^ *\\includegraphics{[^}]*} *$ RET
\\begin{center} \& \\end{center} RET

m121212

unread,
Nov 25, 2009, 6:58:22 AM11/25/09
to Help-gn...@gnu.org

Thanks for your repsonses! The solution for me in the end was just to record
a keyboard macro. Do you use a particular guide for regexps? Also, could
you explain this part: {[^}]*} *$ RET ?

M-x replace-regexp RET
^ *\\includegraphics{[^}]*} *$ RET
\\begin{center} \& \\end{center} RET


Thanks,
m121212
--
View this message in context: http://old.nabble.com/regexp-emacs-selective-replace-tp26478209p26511406.html

Joost Kremers

unread,
Nov 25, 2009, 7:11:50 AM11/25/09
to
m121212 wrote:
>
> Thanks for your repsonses! The solution for me in the end was just to record
> a keyboard macro. Do you use a particular guide for regexps?

(info "(emacs) Regexps")

> Also, could
> you explain this part: {[^}]*} *$ RET ?

see the above info node. ;-) { and the final } are just literal characters,
[...] describes a set of alternatives. ^ negates such a set, so that [^}]
matches any character other than }.

note that {[^}]*} could also have been written as {.*?} (which IHMO is a bit
clearer and easier to read).


--
Joost Kremers joostk...@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

harven

unread,
Nov 25, 2009, 7:24:57 AM11/25/09
to
m121212 <m12...@mailinator.com> writes:

> Thanks for your repsonses! The solution for me in the end was just to record
> a keyboard macro. Do you use a particular guide for regexps? Also, could
> you explain this part: {[^}]*} *$ RET ?

{[^}]*} *$ means

{ first an opening brace,
[^}]* then any number of characters that are not a closing brace
} then a closing brace,
* any number of spaces,
$ and the line ends there
RET RETURN to proceed

This matches {xxx}
but not {xxx} \end{center} because the line does not end after the brace.

See http://www.emacswiki.org/emacs/RegularExpression

Joost Kremers

unread,
Nov 25, 2009, 7:32:20 AM11/25/09
to
harven wrote:
> {[^}]*} *$ means
[...]

> This matches {xxx}
> but not {xxx} \end{center} because the line does not end after the brace.

and because there would be a } in the matching string.

harven

unread,
Nov 25, 2009, 7:36:43 AM11/25/09
to
Joost Kremers <joostk...@yahoo.com> writes:

> note that {[^}]*} could also have been written as {.*?} (which IHMO is a bit
> clearer and easier to read).

strictly speaking {[^}]*} is not the same as {.*?}
The first will match

{xxx
}

because emacs regexp [^...] matches newline if it is not included in the
brackets. By the way {.*?} is probably closer to what the OP asks for.

Joost Kremers

unread,
Nov 25, 2009, 7:44:25 AM11/25/09
to
harven wrote:
> Joost Kremers <joostk...@yahoo.com> writes:
>
>> note that {[^}]*} could also have been written as {.*?} (which IHMO is a bit
>> clearer and easier to read).
>
> strictly speaking {[^}]*} is not the same as {.*?}

when it comes to regexps, all that counts is "strictly speaking". ;-)

> The first will match
>
> {xxx
> }
>
> because emacs regexp [^...] matches newline if it is not included in the
> brackets.

yup, true. forgot about that...

Xah Lee

unread,
Nov 26, 2009, 2:20:34 PM11/26/09
to
On Nov 23, 5:41 am, m121212 <m121...@mailinator.com> wrote:
> Hi,
>
> I have a tricky problem that I'm not sure how to solve. I have a latex
> document with several figure environments that look like this:
>
> \begin{figure}
> \includegraphics{blah.ps}
> \end{figure}
>
> but need to look like this:
>
> \begin{figure}
> \begin{center} \includegraphics{blah.ps} \end{center}
> \end{figure}
>
> Some of the figure environments already have this however, and I don't want
> to end up with something like this:
>
> \begin{figure}
> \begin{center}\begin{center} \includegraphics{blah.ps}
> \end{center}\end{center}
> \end{figure}

This page answers your question exactly:

• Elisp Lesson: Repeated Find Replace
http://xahlee.org/emacs/elisp_repeat_replace.html

if you prefer emacs to ask you for each case, perhaps just to be sure
your regex didn't find a bad match, see:

• Lisp Lesson: Regex Replace with a Function
http://xahlee.org/emacs/lisp_regex_replace_func.html

> Any ideas? Also, is there a way Emacs can just wrap any selection with
> custom, predefined tags?

(defun wrap-markup ()
"Insert a markup <b></b> around a region."
(interactive)
(goto-char (region-end)) (insert "</b>")
(goto-char (region-beginning)) (insert "<b>")
)

the above is from a collection of simple elisp examples at

• Emacs Lisp Examples
http://xahlee.org/emacs/elisp_examples.html

Xah
http://xahlee.org/


0 new messages