Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Simplifying regexes

by ExReg (Priest)
on Oct 26, 2015 at 17:23 UTC ( [id://1146007]=note: print w/replies, xml ) Need Help??


in reply to Re: Simplifying regexes
in thread Simplifying regexes

Reading the replies above, I get the impression that looking at the theoretical Regular Expression material, while probably good to learn, is limited in its applicability to Perl regular expressions. It would appear that the path to enlightenment is through practice, and not pedagoguery.

Replies are listed 'Best First'.
Re^3: Simplifying regexes
by Laurent_R (Canon) on Oct 26, 2015 at 17:56 UTC
    Reading the replies above, I get the impression that looking at the theoretical Regular Expression material, while probably good to learn, is limited in its applicability to Perl regular expressions.
    That's pretty much what I wanted to say. Yes, it is probably interesting to look at theoretical regular expression, but it will have little value for your practical problem, because our Perl "regexes" are anything but regular.
Re^3: Simplifying regexes
by AnomalousMonk (Archbishop) on Oct 27, 2015 at 08:55 UTC
    It would appear that the path to enlightenment is through practice, and not pedagoguery.

    This has been my experience. "Regular expressions," as they have evolved (into ir-regular expressions, as others have noted), are the most counterintuitive things I have encountered in programming.

    Here's my favorite example of this conceptual orneriness. What will the regex  /(b*)/ capture when run against the string  'aaaaabbbb' and where (i.e., at what character position offset)? Knowing as we do that  * matches "as much as possible" of something, I'd almost be willing to bet money that even the most regex-savvy will experience at least a minor knee-jerk twitch in the general direction of "it matches  'bbbb' at offset 5." But surprise, surprise:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'aaaaabbbb'; print qq{matches '$1' at offset $-[1]} if $s =~ /(b*)/; " matches '' at offset 0
    It matches our old friend the empty string at a location as far away from any  'b' as it could possibly get.

    The answer to the puzzle is that "as much as possible" cheats and leaves out the leftmost and equally important part of the "leftmost longest" incantation that should properly be used* to describe all regex matching. Bottom line: No amount of theoretic or pedagogic vaccination can build up your natural antibody resistance to this sort of thought-bug better than daily exposure to a wide variety of regex challenges. Good luck in your experimentation, and may you make many mistakes, for I know no better way to learn this stuff.

    * Old Whateley and his son Wilbur knew the dangers of incomplete and possibly maliciously altered incantations.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1146007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found