in reply to Regex partial/leading match

PCRE stands for Perl Compatible Regular Expressions. It is a library that has been written to enable users of other languages to use the power of Perl's regular expressions. So, PCRE is just mimicking the Perl regular expressions, and, basically, anything you can do with PCRE can be done with Perl. In other words, if you really know how to do it in C with PCRE, then you should be able to do it very easily in Perl.

The issue we have here is that your problem is not very well defined, so that it is difficult to suggest a solution.

If I understand your issue correctly, you are not mastering the regexes that you are going to use, they are provided to you by an external source. If such is the case, then you can't do very much about it, it is the author of these regex that ought to provide you with the proper set of patterns.

Well, a general solution to this problem is probably out of reach, but I am also not saying that it can't be done. In theory at least, you might be able to parse the patterns supplied to you to build beginning-of-string regex sub-patterns provided those patterns supplied to you are relatively simple and very well defined, but that is not very easy and probably not a very robust solution, because you're probably bound to fail if the author of the patterns provided to you decides to start to be a bit too clever. OTOH, if you can define a very limited subset of authorized patterns, then it can probably be done. The fact that you seem to be willing to analyze directory paths is certainly essential to a possible definition of such simple patterns provided to you and therefore to a possible solution to your problem.

But you don't give us enough information for us to really be able to help you much further.

Replies are listed 'Best First'.
Re^2: Regex partial/leading match
by Anonymous Monk on Jan 02, 2016 at 13:14 UTC

    No. And no.

    The OP did provide enough information. He may be "guilty" of abusing the regex for a task typically solved otherwise, but the problem is understood and so is the desire for it. Variations on the theme have been posted before: "can I determine the point of matching failure? The longest tentative match?"

    I'm not intimately familiar with the guts of rx matching, but here's how I reckon this: the rx engine does its best to try to avoid any sort of delay or inefficiency in backtracking. It does not remember where it fails. So this feature is something that PCRE does support and perl does not.

    One "solution" that might work for the OP, is to rewrite the pattern and inject

    (?(?{pos==length})(*ACCEPT))
    before every \\, but that's neither generic nor tidy.