Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regex, the jungle survival skill

by mmkstarr (Initiate)
on Mar 10, 2016 at 05:36 UTC ( #1157253=perlquestion: print w/replies, xml ) Need Help??
mmkstarr has asked for the wisdom of the Perl Monks concerning the following question:

I am wondering how far one can get in pearl without learning regex? Is regex fluency a huge boon to a perl user? If so, what are some of the best regex tutorials you have come upon?

Replies are listed 'Best First'.
Re: Regex, the jungle survival skill
by kcott (Chancellor) on Mar 10, 2016 at 07:22 UTC
Re: Regex, the jungle survival skill
by Athanasius (Chancellor) on Mar 10, 2016 at 06:29 UTC

    Hello mmkstarr, and welcome to the Monastery!

    You can program in Perl without using regular expressions. On the other hand, Perl’s built-in regular expression engine is extremely powerful — in fact, it’s one of Perl’s main features — so any effort you put into learning Perl regexen will be richly repaid as you put them to use in your scripts.

    The official tutorial is a good starting point: perlretut. After that, you should read through the “Pattern Matching” chapter of the Camel Book,1 although you can leave the final section, “Fancy Patterns,” till later. But, of course, the only way you’ll really learn regexen is by trial and error: write your own, experiment, and learn from your mistakes!

    BTW, the language is called “Perl” (capital P, no A); the interpreter is called “perl” (no capital).

    1Chapter 5 of Programming Perl (4th Edition, 2012) by Tom Christiansen, brian d foy & and Larry Wall with Jon Orwant.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Regex, the jungle survival skill
by dasgar (Priest) on Mar 10, 2016 at 07:09 UTC

    If you're interested in getting a good reference book on regular expressions in general, you might want to get a copy of Mastering Regular Expressions by Jeffrey Friedl. It may not necessarily be the best place to start learning, but it is a very good reference book to own.

    As for how useful regular expressions are, I still remember my reaction to an example from that book. I can't remember all of the details but the example was describing the task of finding double occurrences of words (such as "the the" while ignoring case differences. And it should include cases of last word of one line and first word of the next line being the same. And the task was not just finding these occurrences, but highlighting them and copying those lines into another file. My initial thought was that had to be hundreds of lines of code, but the author said it could be done in something like 5 lines of code. At that time, I just couldn't believe that such a complicated task could be accomplished with very little code. When I read that, I knew that I really needed to learn regular expressions.

Re: Regex, the jungle survival skill
by 1nickt (Monsignor) on Mar 10, 2016 at 06:28 UTC

    I use regular expressions in almost every program in one way or another.

    Start at the beginning: perlrequick.


    The way forward always starts with a minimal test.
Re: Regex, the jungle survival skill
by Laurent_R (Canon) on Mar 10, 2016 at 07:30 UTC
    Jeffrey Friedl's Mastering Regular Expressions is an excellent book, but certainly not the place to start with if you're going to start studying regexes.

    Otherwise, just to confirm, regex is really a central skill for using Perl, because it is so deeply integrated within the language. Perl is know to be a language of choice for manipulating text data because of regexes, and it don't use them, your abilities to manipulate data becomes actually quite poor, because many things that can be done with special functions on other languages have to be done with regexes in the broader sense in Perl.

    And, BTW, it is much less difficult to learn than you might think: regex may seem impressive if you don't know them, but there is nothing really difficult about them.

Re: Regex, the jungle survival skill
by stevieb (Abbot) on Mar 10, 2016 at 13:24 UTC

    Great responses by the other Monks, so I'll just add a couple of things. A couple of debugging tools that can help you understand regexes better are the built-in regex debugger, use re 'debug';, and YAPE::Regex::Explain, both will allow you to display what your regexes are doing, or paste in regexes you find elsewhere and explain to you how they work.

Re: Regex, the jungle survival skill
by Your Mother (Chancellor) on Mar 10, 2016 at 15:07 UTC

    Then, when you're ready to sink a couple dozen hours having "fun" with regexen: http://regex.alf.nu/. I remember using another good one of these (kinds of sites) but can't seem to find it again.

      Wow! Before I got bored, I tried to play for a while:
      Warmup (207) Anchors (206) Ranges (199) Backrefs (201) Abba (191) A man, a plan (169) Prime (256) Four (199) Order (194) Triples (560) Glob (348) Balance (13) Powers (0) Long count (250) Alphabetical (0) Powers 2 (10) You have 3003 points.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      That's an awesome little tool there :)

Re: Regex, the jungle survival skill
by graff (Chancellor) on Mar 10, 2016 at 14:45 UTC
    It's worth pointing out that most of what you learn about Perl regular expressions will be useful knowledge for many other areas of programming, database work, etc, because regular expressions are used in many places other than Perl.

    (I say "most of what you learn", because I think the Perl regex engine is more extensive - offers a lot more stuff - than other implementations. Still, all the basic concepts and syntax are portable to other scripting languages, SQL and unix shell commands, among other things.)

Re: Regex, the jungle survival skill
by Mr. Muskrat (Canon) on Mar 10, 2016 at 16:40 UTC
Re: Regex, the jungle survival skill
by davies (Parson) on Mar 10, 2016 at 14:44 UTC

    You can do a lot of programming without using a regex. I know. I did it. But if you use sites like this, you will see posts that mention regexes and then, when you are trying to write something, you will realise that a regex is the best tool. That's the best tutorial you can find - a need to solve a problem of your own. Unless you have to pass an exam, don't try learning something you don't use, as you will quickly forget what you learned. Once you get to wanting to learn something, try writing the regex yourself. If you get stuck, post here with what you are trying to do and what you have written. When I've done this, I've found the Monasteriat extremely helpful.

    As others have said, some regexes are very simple. But as I think you realised before the other posts, regexes are a very powerful tool (I don't know them at all well, but I know that much). And in addition to the tools others have mentioned, I have recently taken to using Damian Conway's Regexp::Debugger, which I have found most helpful.

    Regards,

    John Davies

Re: Regex, the jungle survival skill
by perlfan (Curate) on Mar 10, 2016 at 16:13 UTC
    You don't have to know it to get started doing baby Perl, but you should not shy away from regular expressions. It is an early rite of passage on your way to using Perl as the swiss army chainsaw it is.
Re: Regex, the jungle survival skill
by mmkstarr (Initiate) on Mar 11, 2016 at 04:17 UTC
    Wow! So many replies! Really not used to that. Thank you so much! I will work through some of the literature you've provided in the near future.
Re: Regex, the jungle survival skill
by perlfan (Curate) on Mar 11, 2016 at 18:25 UTC
    BTW, people have mentioned Mastering Regular Expressions, but there is an excellent chapter on regular expressions in brian_d_foy's Mastering Perl that I have found particularly enlightening.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1157253]
Approved by Athanasius
Front-paged by davies
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-12-18 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (495 votes). Check out past polls.

    Notices?