in reply to What does it mean that a "pattern cannot be reversed?"

Simple patterns can be reversed: /foo/ can be reversed to /oof/.

Many more complex patterns can be reversed, though it isn't always obvious whether they'd match in exactly the same way: consider for example:

"mississippi" ~~ /p<after (i.*?p)>/; # reverse to /(p.*?i)/ ?? # or maybe /(p.*i)/ ??
in which you might expect the parens to catch "ississip" (ie start the match at the earliest available position) rather than "ip".

However other examples cannot be reversed at all: in general, this might include anything that involves code assertions. Precisely what constitutes a reversible pattern has not yet been fully characterised, and to some extent it will depend on what we decide are the invariants that must be preserved as in the example above.

Hugo

Replies are listed 'Best First'.
Re^2: What does it mean that a "pattern cannot be reversed?"
by TimToady (Parson) on Jan 07, 2005 at 16:57 UTC
    I think it should keep .*? as .*? on the theory that the actual underlying abstraction is "longest/shortest", not "earliest/latest".

    I think code assertions can still work unless they make assumptions like "$1 is always bound before $2". (And I don't think we should reverse $1 and $2 for them. Reversing a lexical scope would be evil.)

    But basically, I don't know what the limits are yet. All I know is that Perl 5 sets them too tight, and we can push them out a little. Rule matching can be more powerful than a locomotive, but there will always be a bit of kryptonite in the world. Language designers tend to concentrate on the locomotive rather than the kryptonite.

      FWIW, relevant reading to this issue at sexeger.

      SNIP:

      "If we're able to look at the specific nodes of a regular expression, then we should be able to create a regex that works from the BACK of a string to the FRONT. What do I mean?

      ....."