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

Please advice what it do ??

Recommended Answers

All 6 Replies

$abc =~ /^           # Beginning of line
         [^\d]{2,4}  # 2 to 4 characters as long as they are not numbers
         <           # a literal less than sign
         ([^>]+)     # at least one and all characters except (essentially until) a
                     # greater than sign.  This text will be remembered.
                     # The parens do that.
         >           # a literal greater than sign
         \d?         # a number is allowed, but doesn't have to be found
         \1          # the "remembered" text 
         $           # end of string

just "matching" not changing anything.

can you describe more , what "matching" are doing ???

$abc =~ /^           # Beginning of line
         [^\d]{2,4}  # 2 to 4 characters as long as they are not numbers
         <           # a literal less than sign
         ([^>]+)     # at least one and all characters except (essentially until) a
                     # greater than sign.  This text will be remembered.
                     # The parens do that.
         >           # a literal greater than sign
         \d?         # a number is allowed, but doesn't have to be found
         \1          # the "remembered" text 
         $           # end of string

just "matching" not changing anything.

Can you descirbe more , what "matching " is doing ???

determining whether the "current" line fits that pattern. I don't know what you understand by "matching", but that is what it means.

determining whether the "current" line fits that pattern. I don't know what you understand by "matching", but that is what it means.

So, what will match if it use below $abc ??

my($abc) = "fred<hello>3hello";

What do the regular expression try to match/do ??

Nothing, except determine whether or not the string matches. Since you are not evaluating that boolean result, it does not do anything. One "side-effect", of course, is the capture group (remember, the "remembered text"). That is also usable after the regex line, as in that "$1" you see later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.