1

The following Perl example is part of long Perl script.

This script takes the results from ifconfig -a and prints the IP address.

Can someone explain how $1 gets the IP address?

And what the regular expression

$RESULTS =~ /addr:(\S+)\s+/

means?

  my $COMMAND = "ifconfig -a | grep inet | grep -v 127.0.0.1 | head -1";
  my $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";
Borodin
  • 126,100
  • 9
  • 70
  • 144
maihabunash
  • 1,632
  • 9
  • 34
  • 60

3 Answers3

5

If a =~ match expression is true, the special variables $1, $2, ... will be the substrings that matched parts of the pattern in parenthesis. $1 matches the first left parenthesis, $2 the second left parenthesis, and so on.

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

So in your regex it matches addr:(any non-whitespace character 1 or more time)matches any whitespace character one or more time. And $1 in capturing the value in parenthesis.

See this question to understand $1: What does $1 mean in Perl?

Community
  • 1
  • 1
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • very intersting but how $1 take only the IP address and not all the addr:106.13.4.9 string? – maihabunash Jul 07 '15 at 11:24
  • 1
    Because `$1` only store what is in parenthesis not outside of parenthesis. Have a look of given link, you will understand behaviour of `$1`. – serenesat Jul 07 '15 at 11:28
1

=~ is the matches operator in perl and evaluates to true if a string (here $RESULTS) can be matched with a regular expression (here /addr:(\S+)\s+/)

When a regular expression is matched in perl, variables are automatically assigned:

  • $& holds the part matched by the whole expression
  • $1 holds the part matched by the first capture group (set of parantheses)
  • $2 the part by the second capture group
  • and so on ...
  • can you please also expalin the syntax in the exp ( as \S+ and \s+/ ) etc – maihabunash Jul 07 '15 at 11:06
  • 1
    `$0` contains name of the file containing the perl script being executed. `$&` returns the entire matched string. – Arunesh Singh Jul 07 '15 at 11:12
  • `\S+` means "1 or more non-whitespace characters" and `\s+` means "1 or more whitespace characters", the forward slashes are just delimiters that mark the beginning and end of the regex, the parentheses capture what matches the pattern inside them and stores it in `$1` in this case, the rest are literal characters. This regex is used to capture everything following `addr:` up to the first whitespace character (e.g. `106.13.4.9`). – MC93 Jul 07 '15 at 11:21
1

$1, $2, $& etc will capture the value the last successful match.

\S+ matches any and negates \s(whitespace).

serenesat
  • 4,611
  • 10
  • 37
  • 53
Sant
  • 53
  • 7
  • 3
    `&` do not use to capture anything. If you mean to write `and` then write `and`. There is special meaning of [&](http://stackoverflow.com/a/8915304/4248931) in perl. – serenesat Jul 07 '15 at 13:19
  • there i missed $. I mean that $& – Sant Jul 11 '15 at 10:19