Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

book vs. web for Perl Regex study?

by FluffyBunny (Acolyte)
on Sep 01, 2010 at 20:55 UTC ( [id://858414]=perlquestion: print w/replies, xml ) Need Help??

FluffyBunny has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I studied C# and Java before.. and I HAVE NEVER confronted Regular Expression until I started learning Perl!!

I don't know if my brain is rusty or not, but I can hardly understand Regex.. How should I self-study Regex? Reading Perl related books might be helpful? or should I use websites? Is it okay for me to depend on Regex programs? I need your advice :3

OMG IT'S PERL!!

~(o.o~) (~o.o)~

Perl and a blind date both require regular expression.. -_-

Replies are listed 'Best First'.
Re: book vs. web for Perl Regex study?
by ww (Archbishop) on Sep 01, 2010 at 21:20 UTC
    perldoc.perl.org/perlretut.html might be a good place to start.
    or
    this uk university site
    or
    the Enginsite site

    Better yet, perhaps, browse all of those, playing with regexen in their examples and puzzles; then -- when you start to get comfortable -- get Friedl. As prior respondent said, it's very worthwhile to have -- on a (handy, nearby) shelf -- but (IMO, ymmv) is not really a great place to start, in part because of its unusual markup, which adds a learning curve to the Regex learning curve.

    That said, you could do far worse than buying a copy now, and see if it works for you as a tutorial.

    Updated: For clarity, changed word order in 2nd sentence of first narrative graf.

      Lots of good ideas here. I will simply suggest that, when you start writing regexes, pick one tool and stick with it. And Perl seems to have the most comprehensive regex engine of all, so it's a great tool to use for learning regexes.

      As Friedl points out, the implementation of regexes is somewhat tool-specific. Don't do like I did and expect your Perl regexes to work in vi, or Java, or Oracle, or other regex-enabled tools. Often they won't until they're modified.

      Then again, don't panic. It's mostly knowing when you need to escape a character, but it still made me crazy for a while. Learn the basics using one tool, on one platform, and only then start crossing tools and platforms.

      Just one dumb old DBA's opinion...

Re: book vs. web for Perl Regex study?
by dasgar (Priest) on Sep 01, 2010 at 21:07 UTC

    I'd recommend that you check out Mastering Regular Expressions by Jeffrey Friedl (published by O'Reilly), which is what I used to get started on learning regular expressions myself. Even if you don't actually end up reading it cover to cover, it's a good reference book to have handy.

      After reading some of the other responses, I've gone back and reflected on my answer. I'm forced to agree with others that this book may not necessarily be the best starting point for some individuals.

      In my case, reading Friedl's book reminded me of a senior level computer science course that I had to take (Formal Languages and Automata). While taking the course, I struggled to understand how the concepts would apply to programming. Friedl's book connected the dots in my mind.

      Regardless of your starting point, I'd recommend doing some experimenting on your own. At least for me, I couldn't fully wrap my mind around regexes without actually doing some hands on learning.

      Mastering Regular Expressions is *the* book. It emphasises praxis and real world examples. TIMTOWTDI in this as in everything else perl related but do not doubt your first instincts. :)
Re: book vs. web for Perl Regex study?
by graff (Chancellor) on Sep 02, 2010 at 01:36 UTC
    To amplify a bit on what JavaFan said above: no matter how you happen to get started, you really really should make it a point to become familiar with the man page and use it regularly -- particularly the one that comes with the particular version of perl that you happen to be using. That is, get into the habit of using the command line perldoc perlre. The output is long, but the payoff is great.

    The reason is that the man page is intended to be the official reference. Care is taken in each perl release to confirm that the man page in that release fully reflects the current state of the regex engine in that release.

    And you don't have to worry about carrying books around with you, or having web access, in order to look stuff up. If you are using perl at a given moment, you have access to the regex man page at that moment. So learn to use it, no matter what other resources you might want to use.

Re: book vs. web for Perl Regex study?
by planetscape (Chancellor) on Sep 02, 2010 at 07:55 UTC
Re: book vs. web for Perl Regex study?
by davies (Prior) on Sep 01, 2010 at 23:39 UTC
    I can answer your question "book or web" in one word: "No".

    Calm down - I'll explain what I mean.

    Like you, when I started learning Perl for fun, I found the regex parts incomprehensible. I skimmed them, certain that they were for other people. Then I started on a project that was manipulating text - not a lot, but the skim had taught me enough to know that the old Basic tools weren't as good as what Perl had. This is when I started learning about regexes, as opposed to reading about them. I had an application for them, not merely the contrived (and apparently pointless) examples that appeared in the books and on the web pages I read. Then I started having problems. The books told me enough to know that it was possible, but not how to do it. So I started preparing a SOPW. Cutting the code down to a minimum (always a good idea), I solved my own problem. This happened several times (it's, for me, a common occurrence in many areas), until I had solved all my problems and had working code.

    To come back to my original "No", I disagree with your use of "or". I would strongly recommend "and" instead - books and web pages and usenet and perldocs and anything else you can think of. As RedElk wrote, only you can tell what works best for you. Me? I'm a bookworm, but I'll post SOPW questions here any time the books don't get me where I want to be. Your balance will probably be different from mine. You may prefer different books and web sites from me. But I'd strongly recommend some balance, and equally strongly not trying to do more than raise your own awareness until you have something you want or need to do with regexes.

    Regards,

    John Davies
Re: book vs. web for Perl Regex study?
by toolic (Bishop) on Sep 02, 2010 at 00:14 UTC
    Is it okay for me to depend on Regex programs?
    Yes. YAPE::Regex::Explain is a handy tool for debugging Perl regular expressions. You can use it to develop your own regex or to decipher someone else's.
Re: book vs. web for Perl Regex study?
by JavaFan (Canon) on Sep 01, 2010 at 22:19 UTC
    I wonder why you didn't mention "reading the various regexp related manual pages" as one of your options. (But in your defense, none of the previous repliers did either).

    That's how I started.

Re: book vs. web for Perl Regex study?
by RedElk (Hermit) on Sep 01, 2010 at 22:45 UTC

    If you really need something to break the rust loose you might consider Chapter 5 of Beginning Perl.

    Either book or web is good, depending on what works best for you.

Re: book vs. web for Perl Regex study?
by dHarry (Abbot) on Sep 02, 2010 at 11:20 UTC

    How can you study Java and not encounter regular expressions??? Maybe it's time to take the lesson again:P

    I was delighted when it was added, though I think it's fair to say that compared to Perl the Java way of working with it is (a bit...) cumbersome. Take for example the following snippet.

    import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class BLA { public static final String BLA_PATTERN = "\"([^\"]+?)\",?|([^,]+),?| +,"; private static Pattern blaRE; public BLA() { blaRE = Pattern.compile(BLA_PATTERN); } public List<String> parse(String line) { List<String> list = new ArrayList<String>(); Matcher m = blaRE.matcher(line); while (m.find()) { // do stuff } return list; } }

    In Perl this would be just one line:)

Re: book vs. web for Perl Regex study?
by sundialsvc4 (Abbot) on Sep 02, 2010 at 21:17 UTC

    As you get your feet wetter in this business, you’ll find yourself fairly constantly dealing with the same sort of challenges ... ripping apart the contents of a file, looking for certain things.   That’s why the Unix environment (in particular) has so many tools that specialize in this ... grep, sed, awk, and of course, Perl itself.

    (As we know, Perl came to be, because a guy named Larry Wall decided that awk was not quite what he wanted, and he was itching to write a language-based tool and he knew how to do it.   The reason for his working with awk in the first place was ... yep, you guessed it.   Lots of text files with important things in them.   Perl developed its initial “buzz” partly because so many other programmers are always in the very same boat.)

    In my experience, the biggest hurdle that stands in the way of comprehension is a regex’s visually baffling syntax.   It looks like “a nest of chicken-scratches,” and it basically is.   And yet, regexes follow a very well thought-out syntax ... it’s just a syntax that happens to be composed of punctuation marks, not words.   Regexes probably do not look like anything that you have seen before, but once you understand just a few principles of this “language,” you will recognize for yourself just how powerful and useful they really are.

    Now that you have discovered this bridge ... cross it now.   You will be glad that you did.   You will use this facility, in various guises, throughout your career ... and you will save bucketfuls of your own time in so doing.   Regular expressions are a “core competency” with many very practical benefits.   What would otherwise be very nasty subprograms ... hard to write and even harder to debug ... are regular expressions, instead.  (“W00T!! W00T!!”)

    There are lots of tutorials out there, including some (e.g. hosted by W3C) which let you type in a regex string and try it, right there on the spot.   You see, most programming languages these days (including JavaScript) support regular expressions ... and let the record show that they usually describe their implementation as being “Perl compatible.”   Perl’s implementation is the one that they elected to follow ... imitation in this case being a sincere form of flattery.

      Not only have regular expressions found a place in many programming languages, I was amused to find that the search & replace command in Juniper's JUNOS CLI uses regular expressions too. A core competency indeed, with uses far beyond traditional programming.

      -- Time flies when you don't know what you're doing
Re: book vs. web for Perl Regex study?
by PeterPeiGuo (Hermit) on Sep 01, 2010 at 23:26 UTC

    Both java and c# support regex. The first time I learned regex, was through perl, but that was a while back; Nowadays, regex is commonly supported in lots of languages.

    To answer your question, I think it doesn't matter whether you use web site, or book. My suggestion is to learn it in a more systematic way initially (book or web site) - that probably will take you couple of hours or longer depending on your learning style. After that mainly rely on web sites and manuals.

    Peter (Guo) Pei

Re: book vs. web for Perl Regex study?
by umasuresh (Hermit) on Sep 02, 2010 at 00:59 UTC
    The first hit for regexp on google search engine is a very good site for beginners
    http://www.regular-expressions.info/
    For testing my regular expressions I use notepad++ regexp search mode before I incorporate it in a Perl script. As others have pointed out, a great book to have in your book shelf is Mastering Regular Expressions by Jeffry Friedl.
Re: book vs. web for Perl Regex study?
by didier (Vicar) on Sep 02, 2010 at 07:12 UTC
Re: book vs. web for Perl Regex study?
by tospo (Hermit) on Sep 02, 2010 at 10:02 UTC

    I agree with others here that you should first and foremost get your hands dirty and just play with regexes. Just use a lot of capturing and print out what you just captured like so:

    my $string = "testing, testing, 123"; my @captured = ($string=~/(testing)/g); print "captured: $_\n" for @captured;
    All you need to know here is that the parentheses capture the string that was matched and the array will contain all matched strings from this regex. The /g means "don't stop after first match, keep searching the entire string". Then play with the regex a bit. Be sure to try character groups such as [a-z], try greedy vs non-greedy:
    # match lowercase letters commas and space and get as many as possible + (greedy) $string=~/([a-z,\s]+)/; # same but non-greedy (stop as soon as you can) $string=~/([a-z,\s]+?)/;
    try anchors like ^ (beginning of string) and $ (end of string). That will get you started.

Re: book vs. web for Perl Regex study?
by Anonymous Monk on Sep 02, 2010 at 10:10 UTC
    I learnt the basics from here. I am now able to use regular expressions quite comfortably, but I am not an expert. http://www.anaesthetist.com/mnm/perl/Findex.htm
Re: book vs. web for Perl Regex study?
by Anonymous Monk on Sep 03, 2010 at 08:55 UTC
    perlrequick, perlretut and perlre which usually comes in the perl-doc package are enough to get started.

    A really good resource is <http://www.regular-expressions.info/>. It is not perl-specific(C# and Java both have regex, along with most (all?) popular languages)

    O'Reilly has 3 books, which you might find helpful :

    Regular Expression Pocket Reference, 2nd Edition<http://oreilly.com/catalog/9780596514273/>

    Mastering Regular Expressions, 3rd Edition <http://oreilly.com/catalog/9780596528126/>

    Regular Expressions Cookbook, First Edition<http://oreilly.com/catalog/9780596520694/>

    But mostly, one 'learns' regex by trial-n-error. After many years I still have to test out all but the simplest ones.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://858414]
Approved by ww
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found