linux - Perl regular expression and arguments -


the following perl example part of long perl script.

this script takes results ifconfig -a , prints ip address.

can explain how $1 gets ip address?

and regular expression

$results =~ /addr:(\s+)\s+/ 

means?

  $command = "ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1";   $results = `$command`;   chomp $results;   #          inet addr:106.13.4.9  bcast:106.13.4.255  mask:255.255.255.0   #          inet 106.13.4.9 netmask ffffff80 broadcast 106.13.4.127   if ( $results =~ /addr:(\s+)\s+/ ) {     $ipaddress = $1; } elsif ( $results =~ /inet\s+(\s+)\s+/ ) {     $ipaddress = $1; }  print "ipaddress = $ipaddress\n"; 

if =~ match expression true, special variables $1, $2, ... substrings matched parts of pattern in parenthesis. $1 matches first left parenthesis, $2 second left parenthesis, , on.

\s matches non-whitespace character,
+ match 1 or more times,
\s matches whitespace character (space, tab, newline),

so in regex matches addr:(any non-whitespace character 1 or more time)matches whitespace character 1 or more time. , $1 in capturing value in parenthesis.

see question understand $1: what $1 mean in perl?


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -