Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does this work?

Status
Not open for further replies.

j0nxuser

Technical User
May 4, 2003
31
US
A guy I know wrote this for me. I know what it does, I just don't understand how it works. Can someone help explain it to me? Or point me in a direction to figure it out?

if ($name =~/.*(NAS\d{1,2})$/){
$router = $1;
}

What does the /.* and the \d{1,2})$/) do? And how does it work?
Thank you,
mL
 
Those items are part of a regular expression.

The "." represents any character except the newline character.
The "*" tells the compiling to look for zero or more of the character just previous to the "*".

So, the regex says "Look for zero or more of any character until you get to NAS".

The "\d" represents any digit (0-9). The "{1,2}" tells the compiling to look for a minimun of 1, maximum of two of the character just previous.

So, the regex says "Look for 1 or 2 digits".

All together: "Look for zero or more of any character, followed by NAS, followed by 1 or two of any digit."

The $ sign indicates that the end of the string. So, it is stipulating that the 1 or 2 digits have to be the very last item in the string or else it won't match.

The parentheses around NAS\d{1,2} tell the compiler that you want to save the results of what is found in parentheses. The found results are stored in a special variable. The variable for one set of parentheses is $1. If you had two sets, it would be $1, $2. Each additional set of parentheses adds one to the number in the special variable.


You can find out more about regular expressions here:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top