Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Help with Regex

by Mercio (Scribe)
on Jul 06, 2004 at 19:32 UTC ( #372189=perlquestion: print w/replies, xml ) Need Help??
Mercio has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have been greatly improving my perl skill thanks to the help of my fellow monks and lots of practice. I have been working on creating modules and random scripts to practice new skills.I was recently making a script and a problem I keep running accross is my limited skills with Regex. I don't understand all that much about it, but I would like to learn.

What I am asking is if any monk out there would like to share links or inside tips on the ways you became familiar with Regex. Regex is my worst enemy, but I would like to fix that. Thank You.

Replies are listed 'Best First'.
Re: Help with Regex
by gaal (Parson) on Jul 06, 2004 at 19:40 UTC
    The first place to look is perlretut. There are good bite-sized discussions in the major Perl books (Programming Perl, Learning Perl, The Perl Cookbook). Also check out perlfaq4.

    Then read Mastering Regular Expressions.

Re: Help with Regex
by heroin_bob (Sexton) on Jul 06, 2004 at 19:40 UTC
    I've found the tutorials on this site to be extremely helpful. Also, injunjoel turned me onto chapter 3 of the O'Reilly book Sed & Awk called "Understanding Regular Expression Syntax", which has proven very useful.
Re: Help with Regex
by pbeckingham (Parson) on Jul 06, 2004 at 20:26 UTC

    I have to say that I am still only half way up that learning curve, but perlretut was very helpful. I also have a couple of decades exposure to vi, emacs and other regex-implementing apps, which gave me some familiarity.

    Jeffrey Friedl's book will be a fascinating read to you, though it is not for the faint of heart.

Re: Help with Regex
by Elijah (Hermit) on Jul 06, 2004 at 20:32 UTC
    A very good crib sheet outlining all of perl regexp's functions and uses can be found here. I still refer to this page quiet often.
      Thank you for the crib sheet!

      Perl Monks: Great People - Great Resouces!

      (so many nodes and so little time ... )

Re: Help with Regex
by Mercio (Scribe) on Jul 06, 2004 at 22:22 UTC
    Ok, I've read a few of them and decided to try and take all the html tag names out of a file and print them, however I am running into a few problems. This is what i have.
    $content = "<head><body blah></body><foo></foo></head>"; while ($content =~ /<([^(?:\s|>)]+).*>.*<\/\1>/ig) { print $1."\n"; }
    This works fine as long as the html tags do not encompass other html tags. In this case they do and it will only find html. Is there something I'm doing wrong? I've tried everything.
      Anything but the simplest HTML processing and you should be using HTML::Parser, not regex.


        I agree. Even if it's your own html and you know what to expect. It is never worth it and it will bite back eventually.
        Since I started using HTML::TokeParser I've never looked back. I use it even on "the simplest HTML". Why go to all that effort when others (who know what they're doing) already have?
        The best advice I've seen in regex tutorials is "don't roll your own html parser".
      if you're trying to match that entire string you could try this
      this matches from the first tag to the last tag
      I hope I understood what you we're trying to do
      $content = "<head><body blah></body><foo></foo></head>"; $content =~ m[^(<(.+?)>.*?</\2>)$]; print $1."\n";
      if you just wanted to match and print out the individual tags you could do this
      $content = "<head><body blah></body><foo></foo></head>"; while ($content =~ m[(<.+?>)]g) { print $1."\n"; }
      a great tutorial on perlmonks covering how a regex will match
      hope this helps
Re: Help with Regex
by Mercio (Scribe) on Jul 06, 2004 at 20:09 UTC
    Ok, thanks for the help guys. I'm going to get right to it. I looked at some of them already and they look promising. Thanks again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://372189]
Approved by ysth
[marto]: time for coffee

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-05-21 08:16 GMT
Find Nodes?
    Voting Booth?