in reply to The "anchor" misnomer in regexes

How does your idea of anchors hold when there are more than one? It is easy to see that $string =~ /#(.*?)$/ could be processed as reverse $string =~ /^(.*?)#/ and it would then produce very different and perhaps more intuitive results. But what would you then do with $string =~ /^#(.*?)$/ .. now your anchor would seem to mean different things and behave differently based on the number of anchors. The current anchors hold well enough and arn't so confusing once you get used to it. After all, find a #, take everything after that up to the end of the line, is pretty straight forward. More so than, take your string, start at the end, and find the first # sign. Which you'll notice is the reverse order of the actual regex so you would have to read it backwards.

Just my two cents.


___________
Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;

Replies are listed 'Best First'.
Re^2: The "anchor" misnomer in regexes
by dragonchild (Archbishop) on Dec 17, 2005 at 05:22 UTC
    It is easy to see that $string =~ /#(.*?)$/ could be processed as reverse $string =~ /^(.*?)#/ and it would then produce very different and perhaps more intuitive results.

    Those aren't identical.

    $string = "abc#def#ghi"; ($x) = $string =~ /#(.*?)$/; print "$x\n"; ($x) = reverse($string) =~ /^(.*?)#/; $x = reverse $x; print "$x\n"; ---- def#ghi ghi
    You're still thinking of $ as a starting point versus simply being a zero-width assertion. /#([^#]*)$/ is the one that reverses to /^(.*?)#/. The original regex /#(.*?)$/ is more clearly written as /#(.*)$/. Because you don't have any non-zero-width assertions between the .*? and the $, the non-greedy modifier doesn't actually change any behavior. The reverse of the original is /^(.*)#/ - explicitly greedy.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      I didn't say they were identical, and i can't "still" be thinking of them in any way because its my first post. Reading "How does your idea of anchors hold when there are more than one?" I meant to say that the rest of the post would be regarding how the OP was thinking anchors WOULD be, not how they are. My intention was to show that while that view might make sense with the simple single ended cases, more complex cases make it apparent that the current meaning of anchor is realy the one of least surprise and easiest implementation.


      ___________
      Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;