unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Problem with re-search-backward and "\\="
@ 2003-09-16 16:44 Alan Mackenzie
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Mackenzie @ 2003-09-16 16:44 UTC (permalink / raw)


GNU Emacs 21.1

Suppose I have this in a buffer:

foo     bar

If point is immediately after foo, then (re-search-forward "\\= *")
leaves point immediately before bar, as one would expect.

However, with point immediately before bar (re-search-backward " *\\=")
fails.  I would have expected this search to have succeeded, leaving
point just after foo.

The definition of `\=' in the elisp info page "Regexp Backslash" is:

> matches the empty string, but only at point.  (This construct is not
> defined when matching against a string.)

Is this a bug, or have I misunderstood something?

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with re-search-backward and "\\="
@ 2003-09-16 18:57 Greg Hill
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Hill @ 2003-09-16 18:57 UTC (permalink / raw)


>GNU Emacs 21.1
>
>Suppose I have this in a buffer:
>
>foo     bar
>
>If point is immediately after foo, then (re-search-forward "\\= *")
>leaves point immediately before bar, as one would expect.
>
>However, with point immediately before bar (re-search-backward " *\\=")
>fails.  I would have expected this search to have succeeded, leaving
>point just after foo.
>
>The definition of `\=' in the elisp info page "Regexp Backslash" is:
>
>>  matches the empty string, but only at point.  (This construct is not
>>  defined when matching against a string.)
>
>Is this a bug, or have I misunderstood something?

Alan,

You have misunderstood the nature of the "greediness" of the '*' -- 
and probably also the '+' -- postfix operator when applied to 
backward searches.  It is not "symmetrical" with its effect on 
forward searches.  I suggest you do some experimentation with these 
operators, never minding the "\\=" for the moment, to better 
understand the way these postfix operators work.

I may not have this completely right, but this is the way I 
conceptualize it.  When searching forward, the match-beginning 
advances forward from point until the first possible match is found; 
then match-beginning is fixed and match-end advances until going any 
farther would break the rule or exceed the specified limit.  In 
searching backward, the match-beginning moves backward until the 
first possible match is found; then match-beninning is fixed and the 
match-end advances forward until going any farther would break the 
rule, using the initial value of point as the limit to how far the 
match-end is allowed to advance.

I have never experimented with the "non-greedy" postfix operators 
'*?' '+?' and '??', so I can't tell you how using them effects the 
conceptualization described above.

I hope this helps.

--Greg

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with re-search-backward and "\\="
       [not found] <mailman.313.1063738732.21628.help-gnu-emacs@gnu.org>
@ 2003-09-17  8:13 ` Alan Mackenzie
  2003-09-17 17:39   ` Greg Hill
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Mackenzie @ 2003-09-17  8:13 UTC (permalink / raw)


Greg Hill <ghill@synergymicro.com> wrote on Tue, 16 Sep 2003 11:57:03
-0700:
>>GNU Emacs 21.1

>>Suppose I have this in a buffer:

>>foo     bar

>>With point immediately before bar (re-search-backward " *\\=") fails.
>>I would have expected this search to have succeeded, leaving point just
>>after foo.

>>Is this a bug, or have I misunderstood something?

> Alan,

> You have misunderstood the nature of the "greediness" of the '*' -- 
> and probably also the '+' -- postfix operator when applied to 
> backward searches.  It is not "symmetrical" with its effect on 
> forward searches.  I suggest you do some experimentation with these 
> operators, never minding the "\\=" for the moment, to better 
> understand the way these postfix operators work.

OK, I think I've got it now.  It finds the _minimum_ match it can.  With
my expression, it matches on zero spaces.  It's even documented in the
elisp info pages:  "A true mirror-image of `re-search-forward' would
require a special feature for matching regular expressions from end to
beginning.  It's not worth the trouble of implementing that."  ;-(

> I may not have this completely right, but this is the way I 
> conceptualize it.  When searching forward, the match-beginning 
> advances forward from point until the first possible match is found; 
> then match-beginning is fixed and match-end advances until going any 
> farther would break the rule or exceed the specified limit.  In 
> searching backward, the match-beginning moves backward until the 
> first possible match is found; then match-beginning is fixed and the 
> match-end advances forward until going any farther would break the 
> rule, using the initial value of point as the limit to how far the 
> match-end is allowed to advance.

> I have never experimented with the "non-greedy" postfix operators 
> '*?' '+?' and '??', so I can't tell you how using them effects the 
> conceptualization described above.

I wasn't aware these existed.  Thanks for the tip!

> I hope this helps.

Very much so.  For what I actually need to do (moving back any
combination of whitespace and a few things like "//." as the first
non-space stuff on a line), I can do it easily enough by hand.

Many thanks.

> --Greg

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with re-search-backward and "\\="
  2003-09-17  8:13 ` Alan Mackenzie
@ 2003-09-17 17:39   ` Greg Hill
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Hill @ 2003-09-17 17:39 UTC (permalink / raw)


At 8:13 AM +0000 9/17/03, Alan Mackenzie wrote:
>OK, I think I've got it now.  It finds the _minimum_ match it can.

Well, not exactly.  It finds the longest "rightmost" match it can. 
Searching backward, once it has found a character that can serve as 
the beginning of a complete match, it then still finds the longest 
match possible starting from that point.  For example, starting with 
(point) just in front of the "b" in "fooooo gooo bar", 
(re-search-backward "[fg]o*" nil t) will find and return "gooo", not 
just "go", as the string returned by (match-string 0).

>For what I actually need to do (moving back any
>combination of whitespace and a few things like "//." as the first
>non-space stuff on a line), I can do it easily enough by hand.

It sounds to me like the functions skip-chars-forward, 
skip-chars-backward, skip-syntax-forward and skip-syntax-backward 
will probably serve you well.

--Greg

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-09-17 17:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-16 18:57 Problem with re-search-backward and "\\=" Greg Hill
     [not found] <mailman.313.1063738732.21628.help-gnu-emacs@gnu.org>
2003-09-17  8:13 ` Alan Mackenzie
2003-09-17 17:39   ` Greg Hill
  -- strict thread matches above, loose matches on Subject: below --
2003-09-16 16:44 Alan Mackenzie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).