$catreg = "^(<[A-Za-z0-9]+>)*<H1>(<[A-Za-z0-9]+>)*([A-Za-z0-9]+)";

This places the characters on the right hand side of the equals sign and between the " into a variable called $catreg.

if ( m!$catreg! ) {

This line is a conditional test. IF the bit between the ( ) is true then the block following the condition will be evaluated - this block goes from { to }.

To simplify m!foo! is a regular expresion that will match any string that contains the letters 'foo' in that order.

In your case we have m!$catreg! When perl sees the $catreg it looks to see what the variable catreg contains and uses this content in the regular expression. Thus you are looking for a match like this:

m!^(<[A-Za-z0-9]+>)*<H1>(<[A-Za-z0-9]+>)*([A-Za-z0-9]+)!

Note we are matching against the unspecified default value for regex matches which is stored in the magical $_ variable. In english this reads as follows:

# the lack of a $var =~ here indicates # that we are matching against $_ m # this is a match regex ! # we define the extent of the regex using a ! ^ # starting at the begining of the string ( # grouping and capturing opening bracket < # matches this charachter '<' [A-Za-z0-9]+ # match any number of alphanumerics in a row > # match a literal '>' ) # closing bracket # the match between brackets captures to $1 * # accept 0 or more of the preceeding stuff # but note only first match captured into $1 # within the brackets <H1> # match a literal '<H1>' (<[A-Za-z0-9]+>)* # same as first but captures into $2 # the first '<alphanumeric>' sequence ([A-Za-z0-9]+) # matches sequential alphanumerics and # captures all into $3 ! # end of regex

I'm afraid that this regex does not do anything like what you think it does. It's results are complex, I suggest you try this sample code. Play with the value of $_ to see what I mean.

$catreg = "^(<[A-Za-z0-9]+>)*<H1>(<[A-Za-z0-9]+>)*([A-Za-z0-9]+)"; $_="<a><b><H1><c><d>foo"; if ( m!$catreg! ) { print "Matched \$1:$1 \$2$2 \$3$3\n"; } else { print "No Match\n"; }

If you want to capture the text between <H1> and </H1>,regardless of whether they are <h1> and </h1> then this will work.

$_='<h1>foo</h1>blah<H1>bar</H1>more blah'; while (m!<h1>(.*?)</h1>!ig) { print "Found $1\n"; }

tachyon


In reply to Re: (**corrected**) What Does This Line Do? by tachyon
in thread (**corrected**) What Does This Line Do? by sfrattura

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.