$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 |