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

Multiline regex

44 views
Skip to first unread message

Giles Chamberlin

unread,
Nov 21, 2011, 8:56:15 AM11/21/11
to help-gn...@gnu.org
I'm having trouble with a multi line regex: can any one show me where
I'm going wrong?

I have a data file of the form:

a
1
b

a
2
b

and wish to replace the "paragraphs" a..b. Using re-builder (and based
on http://www.emacswiki.org/emacs/MultilineRegexp) I have constructed a
successfully matching regex:

"a\\(.*
?\\)*?*b"

but when I try to use that regex in replace-regex I get 0 matches. Any
suggestions?




--
Giles


Peter Dyballa

unread,
Nov 21, 2011, 10:41:41 AM11/21/11
to Giles Chamberlin, help-gn...@gnu.org

Am 21.11.2011 um 14:56 schrieb Giles Chamberlin:

> Any suggestions?

Just into the blue: real NEWLINEs might be missing at first line's end. They can be entered with C-q C-j.

--
Greetings

Pete

Only useless documentation transcends the first two laws.
– Arnold's Third Law of Documentation


S Boucher

unread,
Nov 21, 2011, 11:46:17 AM11/21/11
to Giles Chamberlin, help-gn...@gnu.org

----- Original Message -----

> and wish to replace the "paragraphs" a..b.  Using re-builder (and
> based
> on http://www.emacswiki.org/emacs/MultilineRegexp) I have constructed a
> successfully matching regex:
>
> "a\\(.*
> ?\\)*?*b"

The expression, has some weird things.  * means 0-to-n occurences, ? means 0-or-1 occurences.  So, you probably do not want *?*.

Try M-x regexp-builder

It's a nice interactive way to put together your regexp.

Valentin Baciu

unread,
Nov 21, 2011, 10:54:39 AM11/21/11
to Giles Chamberlin, help-gn...@gnu.org
Hello,

You are probably calling the `replace-regexp' interactively. Try to replace the regexp with: 
a\(.*
?\)*?*b

Otherwise, calling (replace-regexp "a\\(.*
?\\)*?*b", "") from code, in the buffer containing your text should work as expected.

I think this is happening because how strings are being read and escaped. In one case you need the \\ since you are calling the function from code and the backslash has to be escaped. But, when called interactively (using M-x) you are already passing an "escaped" backslash, so one is sufficient.

On Mon, Nov 21, 2011 at 3:56 PM, Giles Chamberlin <giles.ch...@cisco.com> wrote:
I'm having trouble with a multi line regex: can any one show me where
I'm going wrong?

I have a data file of the form:

a
1
b

a
2
b

and wish to replace the "paragraphs" a..b.  Using re-builder (and based
on http://www.emacswiki.org/emacs/MultilineRegexp) I have constructed a
successfully matching regex:

"a\\(.*
?\\)*?*b"

Giles Chamberlin

unread,
Nov 22, 2011, 8:11:24 AM11/22/11
to help-gn...@gnu.org
Valentin Baciu <vale...@syntactic.org> writes:

> You are probably calling the `replace-regexp' interactively. Try to replace
> the regexp with:
> a\(.*
> ?\)*?*b

That was exactly it thanks. I was running this interactively and once
I fixed the escaping all was well. Off to re-read the regex section of
the manual.

--
Giles



0 new messages