-1

I have this string:

/dev/sda1      23G   46G  47G  22%

And want to match only the integer in 47's place.

I tried modifying the "match second set of numbers" example (i.e.,\d+[^\d*](\d+)) here to make it match the third set (e.g., , http://perlmeme.org/faqs/regexp/regexp.html, but I can't get a match.

Any idea how to match only the integer in 47's place?

chicks
  • 3,793
  • 10
  • 27
  • 36
blashmet
  • 19
  • 2

2 Answers2

2

Here's a verbose example:

$string = '/dev/sda1      23G   46G  47G  22%';

my ( $number ) = $string =~ m|^/[\S]+/[\S]+\s+\d+\w\s+\d+\w\s+(\d+)|;

print $number;

Or if you'd like to see the regex more spread out:

my $string = '/dev/sda1      23G   46G  47G  22%';

my ( $number ) = $string =~ m|
        ^/[\S]+/[\S]+\s+    # drive
        \d+\w\s+        # first number - ignored
        \d+\w\s+        # second number - ignored
        (\d+)           # third number - CAPTURED
    |x;

print "$number\n";
chicks
  • 3,793
  • 10
  • 27
  • 36
1

^\S+\s+\S+\s+\S+\s+(\d+) would do the trick, but I'd recommend splitting on whitespace and taking the fourth group, instead.

womble
  • 96,255
  • 29
  • 175
  • 230
  • That worked nicely, thank you. I upvoted but it's not public yet because I don't have enough reputation. Can you elaborate how the splitting whitespace method would work? – blashmet Aug 11 '15 at 04:16
  • Something like `print split(/\s+/, $str)[3]` would, I think, do the trick. – womble Aug 11 '15 at 04:23