Home: Perl Programming Help: Intermediate:
what below code do



abcdef
New User

Feb 9, 2011, 10:31 PM


Views: 4477
what below code do

Hi,

I have below code :



my($abc) = "fred<hello>3hello";
$abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;

if (defined($1)) {
print "$1\n";
} else {
print "not found\n";

}
}

What is the code doing ?? what function of the regular expression

$abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;

??


budman
User

Feb 10, 2011, 3:26 PM


Views: 4464
Re: [abcdef] what below code do


In Reply To
What is the code doing ?? what function of the regular expression

$abc =~ /^[^\d]{2,4}<([^>]+)>\d?\1$/;

??



Code
  /^                       start of line 
[^\d]{2,4} match any non-digit (chars)
containing between 2 and 4 letters
<([^>]+)> inside the angle brackets store the
contents in $1 upto the first
right angle bracket
\d?\1 then check for one digit and
look-ahead (?) for the text we stored
in $1 (\1 is a reference to $1)
$/ with nothing else til the end of the line


Works for
"fr<hello>3hello"
"fre<hello>3hello"
"fred<hello>3hello"

Then prints 'hello' in the case.

"xx<testing>9testing"
"xxx<testing>9testing"
"xxxx<testing>9testing"

These will also work, and print 'testing'