That's quite a complex regex for a newbie. I'll explain by making a somewhat simpler version first:my($abc) = "fred<hello>3hello"; $abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;
First it tries to match 2 to 4 characters that aren't digits (/[^\d]{2,4}/), attached to the front of the string (/^/)./^[^\d]{2,4}<([^>]+)>\d?.*$/;
Then it tries to match something between angle brackets; the thing should not contain a closing angle bracket itself (<([^>]+)>). Notice that there are (unescaped) parens in this part, so the regex engine will capture what it matches, and that'll be the word "hello"; it'll be put into the capture variable $1 because this is the first (actually, the only) set of parens in this regex.
Finally, it tries to match an optional digit (/\d?/); and then something more.
If you try to run it now, you'll see it captures the same thing, in this case.
Your original string is a bit more complex in that the final part must match "\1". This is something special, and it's not chr(1) (that would have been a second possible interpretation): it can only match the string that is in $1 earlier in this match ("hello", remember?). Note that /$1/ would not work: the regex engine would plug the current value of the variable $1 into the regex before it starts to try to match anything; that value would not change afterwards.
Also note that \1 will only match literal strings: this is not a regex. Using a variable in a regex would treat its contents as a regex. Using \1, it is as if quotemeta is applied to the contents of $1 before using it in the regex.
TL;DR:
In reply to Re: perl code question
by bart
in thread perl code question
by abcdef
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |