comp.lang.ada
 help / color / mirror / Atom feed
From: Hugo van der Sanden <hv@crypt0.demon.co.uk>
Subject: Re: Search Up
Date: 2000/11/22
Date: 2000-11-22T00:00:00+00:00	[thread overview]
Message-ID: <3A1BE4CA.24196926@crypt0.demon.co.uk> (raw)
In-Reply-To: 3A1BF2E3.F2C51F59@worldnet.att.net

srini wrote:
> I am developing an editor. It has the ability to "search" for a given
> regular expression. Now, I would like to add the ability to search
> backwards from a given position. I guess it is really not backwards but
> the last occurance of the expression.
> 
> Perhaps I can reverse the string, reverse the regex and then compile the
> regexp and search. Will this be valid?

That depends a lot on what style of regexps you support: if you were
supporting all the regexp features of the latest version of perl, for
example, you would not be able to do this. For smaller subsets it may be
possible: ^, $, |, ., +, *, ?, {m,n}, [class] should all be reversible,
I think; backreferences are where it starts to get much trickier.

If you must find exactly the same matches that a forward search would
find, this will fail on overlapping matches: "abaab" =~ /a./ will match
the final "ab" on a reverse search, where it would have matched the "aa"
on a (repeated) forward search. In that case, you might want to do the
best you can by searching backwards a line at a time until you find a
line that matches, and then search forwards repeatedly to find the last
match on the line. (Of course, this may still fail to find the correct
substring for overlapping matches that cross line boundaries.)

HTH,

Hugo




  reply	other threads:[~2000-11-22  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-22  0:00 Search Up srini
2000-11-22  0:00 ` Hugo van der Sanden [this message]
2000-11-22  0:00   ` r_srinivasan
2000-11-27  0:00   ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox