Re: [pcre-dev] PCRE feature request

トップ ページ
このメッセージを削除
著者: Philip Hazel
日付:  
To: M.Taylor
CC: pcre-dev
新しいトピック: Re: [pcre-dev] PCRE feature request - "backwards" search
題目: Re: [pcre-dev] PCRE feature request
On Thu, 16 Sep 2010, M.Taylor wrote:

> I would really like to provide backwards RE searches, and to do this I
> would need a change made to PCRE (or at least I think I would - please
> correct me if I am wrong here).

...
> But all I need is this: given a string to be searched and RE to search
> for, then return the last match instead of the first. That's it.


There are two possible interpretations of "backwards searches". As well
as the one you have chosen, some people have thought of a totally
backwards search process. For example, for a regex such as "a\d+b" (that
is, "a" followed by any number of digits, followed by "b", they think of
scanning the string from right to left, looking for "b", followed by any
number of digits, followed by "a". In many cases, that might be the same
thing, but one can construct examples where it isn't.

That was just a comment. As to your actual question:

I have had the feature "find the last occurrence of x in a line" in *my*
text editor for a long time, and it works with regex (using PCRE,
naturally). I wrote the code so long ago (it's many years since it was
last modified) that I have forgotten how it works, and will now have to
go and read the code...

Hmm... how cunning (though I say so myself). If the regex is <REGEX>, it
transforms it into .*<REGEX> before compiling and matching. What this
does is to make pcre_exec() zip to the end of the string (because of the
.*), and then come backwards along along the string, looking for a match.

Will this solve your problem?

Philip

--
Philip Hazel