From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,903de1597bba5a2e X-Google-Attributes: gid103376,public X-Google-Thread: f8362,903de1597bba5a2e X-Google-Attributes: gidf8362,public From: Hugo van der Sanden Subject: Re: Search Up Date: 2000/11/22 Message-ID: <3A1BE4CA.24196926@crypt0.demon.co.uk>#1/1 X-Deja-AN: 696656863 Content-Transfer-Encoding: 7bit References: <3A1BF2E3.F2C51F59@worldnet.att.net> Organization: None Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.theory,comp.lang.ada Date: 2000-11-22T00:00:00+00:00 List-Id: 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