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

regexp-builder shows matches, re-search-forward fails?

70 views
Skip to first unread message

Thorsten Jolitz

unread,
Jul 26, 2012, 6:51:36 AM7/26/12
to help-gn...@gnu.org

Hi List,

I constructed a regexp with regexp-builder for a buffer, the matches are
clearly shown, with all the subexpressions in different colors. Then I
do reb-copy and paste the regexp as argument for a re-search-forward
call. In the same buffer I used to build the regexp, re-search-forward
now fails to match anything.

What might be the reason?

--
cheers,
Thorsten



Jambunathan K

unread,
Jul 26, 2012, 6:54:49 AM7/26/12
to Thorsten Jolitz, help-gn...@gnu.org
Check you `point'. Is it already past the matches?

--

Thorsten Jolitz

unread,
Jul 26, 2012, 7:15:32 AM7/26/12
to help-gn...@gnu.org
Jambunathan K <kjambu...@gmail.com> writes:

> Check you `point'. Is it already past the matches?

No, I use (goto-char (point-min)) before the re-search-forward. And I
print out the (current-buffer in the function - its the right one).

BTW its a temp buffer - but that shouldn't matter, right?

--
cheers,
Thorsten


Tassilo Horn

unread,
Jul 26, 2012, 7:17:13 AM7/26/12
to help-gn...@gnu.org
Thorsten Jolitz <tjo...@googlemail.com> writes:

Hi Thorsten,

> I constructed a regexp with regexp-builder for a buffer, the matches
> are clearly shown, with all the subexpressions in different colors.
> Then I do reb-copy and paste the regexp as argument for a
> re-search-forward call. In the same buffer I used to build the
> regexp, re-search-forward now fails to match anything.
>
> What might be the reason?

What's reb-copy?

The problem might be that re-builder wants the regex as string, so you
have to quote backslashes. E.g., re-builder wants "\\(foo\\|bar\\)",
but at the re-search-forward prompt you have to say just \(foo\|bar\)
and emacs does the quoting for you.

Bye,
Tassilo


Thorsten Jolitz

unread,
Jul 26, 2012, 7:23:37 AM7/26/12
to help-gn...@gnu.org
More concrete:

,-------------------------
| \\(^# Task: \\)\\(.*$\\)
`-------------------------

does match both tasks in

,----------------------
| # Task: A+B
|
| (+ (read) (read))
| 3 4
| -> 7
|
|
| # Task: Abstract type
`----------------------

in regexp-builder, but

,------------------------------------------------
| (re-search-forward "\\(^# Task: \\)\\(.*$\\)")
`------------------------------------------------

with point at beginning of that buffer gives 'search-failed'.

Thats strange.
(I tried \( and \\\\( for escaping too, but to no avail)

--
cheers,
Thorsten


Jambunathan K

unread,
Jul 26, 2012, 7:25:19 AM7/26/12
to Thorsten Jolitz, help-gn...@gnu.org
Thorsten Jolitz <tjo...@googlemail.com> writes:

> Jambunathan K <kjambu...@gmail.com> writes:
>
>> Check you `point'. Is it already past the matches?
>
> No, I use (goto-char (point-min)) before the re-search-forward. And I
> print out the (current-buffer in the function - its the right one).
>
> BTW its a temp buffer - but that shouldn't matter, right?

What is your value of `reb-re-syntax'. Is it in read format?

--

Thorsten Jolitz

unread,
Jul 26, 2012, 7:29:30 AM7/26/12
to help-gn...@gnu.org
Tassilo Horn <tas...@member.fsf.org> writes:

> Thorsten Jolitz <tjo...@googlemail.com> writes:
>
Hi Tassilo,

>> I constructed a regexp with regexp-builder for a buffer, the matches
>> are clearly shown, with all the subexpressions in different colors.
>> Then I do reb-copy and paste the regexp as argument for a
>> re-search-forward call. In the same buffer I used to build the
>> regexp, re-search-forward now fails to match anything.
>>
>> What might be the reason?
>
> What's reb-copy?

it copies the regexp from regexp-builder for use in an elisp function,
thats what I need.

> The problem might be that re-builder wants the regex as string, so you
> have to quote backslashes. E.g., re-builder wants "\\(foo\\|bar\\)",
> but at the re-search-forward prompt you have to say just \(foo\|bar\)
> and emacs does the quoting for you.

Thats true, when I use \(foo\|bar\) in M-x re-search-forward it matches
again. But in my function, neither \( nor \\( nor \\\\( work. Maybe
something else wrong with with function - have to check again. Thanks.

--
cheers,
Thorsten


Thorsten Jolitz

unread,
Jul 26, 2012, 7:35:58 AM7/26/12
to help-gn...@gnu.org
Jambunathan K <kjambu...@gmail.com> writes:

> What is your value of `reb-re-syntax'. Is it in read format?

yes.

But I think I got confused, because I used re-search-forward in a while
loop and tried to replace the matches, but the REPLACEMENT didn't work,
the search did - the message "search-failed" was only from the last
iteration in the while loop when no matches were left.

Ok, I got it - thanks for the help and sorry for the noise.

--
cheers,
Thorsten


Eric Abrahamsen

unread,
Jul 26, 2012, 7:36:14 AM7/26/12
to help-gn...@gnu.org
On Thu, Jul 26 2012, Thorsten Jolitz wrote:

> Jambunathan K <kjambu...@gmail.com> writes:
>
>> What is your value of `reb-re-syntax'. Is it in read format?
>
> yes.
>
> But I think I got confused, because I used re-search-forward in a while
> loop and tried to replace the matches, but the REPLACEMENT didn't work,
> the search did - the message "search-failed" was only from the last
> iteration in the while loop when no matches were left.

Just a note, the second optional argument to re-search-forward is
NOERROR: set to 't', it will allow the search to halt quietly. I kind of
wish that was the default.

--
GNU Emacs 24.1.50.1 (i686-pc-linux-gnu, GTK+ Version 2.24.11)
of 2012-07-23 on pellet


Tassilo Horn

unread,
Jul 26, 2012, 9:24:56 AM7/26/12
to help-gn...@gnu.org
Thorsten Jolitz <tjo...@googlemail.com> writes:

>> What's reb-copy?
>
> it copies the regexp from regexp-builder for use in an elisp function,
> thats what I need.

Ah, I didn't know that. And you are using re-search-forward in a
program, not interactively. Well, in that case, the regex syntax are
identically, i.e., you can kill the "<regex>" from the re-builder prompt
and yank it into (re-search-forward "<regex>" nil t) and it should just
work.

Bye,
Tassilo


Tassilo Horn

unread,
Jul 26, 2012, 9:29:40 AM7/26/12
to help-gn...@gnu.org
Thorsten Jolitz <tjo...@googlemail.com> writes:

Hi Thorsten,

> More concrete:
>
> ,-------------------------
> | \\(^# Task: \\)\\(.*$\\)
> `-------------------------
>
> does match both tasks in
>
> ,----------------------
> | # Task: A+B
> |
> | (+ (read) (read))
> | 3 4
> | -> 7
> |
> |
> | # Task: Abstract type
> `----------------------
>
> in regexp-builder,

Ditto here.

> but
>
> ,------------------------------------------------
> | (re-search-forward "\\(^# Task: \\)\\(.*$\\)")
> `------------------------------------------------
>
> with point at beginning of that buffer gives 'search-failed'.

Not for me. I can call exactly that twice and point moves to the end of
the # Task items as it should. The third call then signal no match,
cause there are no more matches.

I used M-: to test that. Maybe you're running the search on the wrong
buffer?

Bye,
Tassilo


Thorsten Jolitz

unread,
Jul 26, 2012, 11:51:04 AM7/26/12
to help-gn...@gnu.org
Tassilo Horn <tas...@member.fsf.org> writes:

Hi Tassilo,

> Not for me. I can call exactly that twice and point moves to the end of
> the # Task items as it should. The third call then signal no match,
> cause there are no more matches.

I figured that out, the search was actually ok, but I made a mistake in
the replacement. Since the whole thing was in a while loop, all
appereances were found, but nothing happened (buggy replacement) and the
last loop gave the error message 'search-failed', so all I saw was that
nothing happened - and that error message, confusing me about the cause
of the error.

--
cheers,
Thorsten


0 new messages