Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

500pts: How can I shorten this regexp ? I cannot figure how to use {7} or somethinh like that here.

$line = `/bin/cat /proc/net/dev | /bin/grep eth0`;

if ( $line =~ m/eth\d+:(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)/ ) {
    print $1.' '.$2;
}


How can I shorten this regexp ? I cannot figure how to use {7} or somethinh like that here.

thanks
0
Octalys
Asked:
Octalys
  • 3
  • 2
1 Solution
 
wlfsCommented:
$line =~ m/eth\d+:(\d+)(?:\s+\d+){7}\s+(\d+)/
0
 
OctalysAuthor Commented:
it works.

What does `?:` means ?
0
 
wlfsCommented:
Take care: The first number after the colon is not guaranteed to start immediately without whitespace. To be failsafe you should use

$line =~ m/eth\d+:\s*(\d+)(?:\s+\d+){7}\s+(\d+)/
0
 
wlfsCommented:
(?:   ) does the same as (   ) except that it doesn't capture anything in $1, $2, ...  It can be used for grouping as in this case.
0
 
OctalysAuthor Commented:
I see, thanks for the answer, I understand it now and it works great.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now