• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

How to capture a number in a string in Perl?

How can I extract 125323 from the following string:

The string is:
Analyze Completed (Errors:125323 Warnings:4)

Open in new window


Captured string:
125323

Open in new window

0
Tolgar
Asked:
Tolgar
  • 6
  • 4
1 Solution
 
farzanjCommented:
my $str = 'Analyze Completed (Errors:125323 Warnings:4)';
my ($errors) = $str =~ /Errors:\s*(\d+)/;

Open in new window

0
 
TolgarAuthor Commented:
Actually, this is the full string

File: /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp has been added.
Files added: 1
Files Added: 0
Files Removed: 0
  C++
    Analyze
      /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp
        Error: Unable to include file 'test_man.h'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 12
        Error: Unable to include file 'iostream'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 13
Analyze Completed (Errors:2 Warnings:0)

Open in new window


and I need the followings:

Since there are two errors, I would like to capture 2 form the last line of the string.

and the actual errors from the string into a string divided by new line character.

So it will be like this:
print $errors;

        Error: Unable to include file 'test_man.h'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 12
        Error: Unable to include file 'iostream'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 13

Open in new window


print $numErrors;
 2

Open in new window

0
 
farzanjCommented:
my (@errors) = $str =~ /(Errors:[^\n\r]+)/g;

$" = "\n";
print "@errors\n";
print @errors. "\n";
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TolgarAuthor Commented:
@farzanj: This does not capture the match. It returns true if there is a match.

my (@errors) = $str =~ /(Errors:[^\n\r]+)/g
0
 
TolgarAuthor Commented:
And the last code you sent just count the number of errors but actually I'd like to capture the number of errors on the last line of the script.

Thanks,
0
 
farzanjCommented:
If all the errors you have are in one string $str, then the above code should capture the error lines.  What did you get?

Second part:
($numErrors) = $str =~ /\(Errors:(\d+)/;

Again the assumption is that the whole thing is in one line $str
0
 
user_nCommented:
$test = 'File: /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp has been added.
Files added: 1
Files Added: 0
Files Removed: 0
  C++
    Analyze
      /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp
        Error: Unable to include file 'test_man.h'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 12
        Error: Unable to include file 'iostream'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 13
Analyze Completed (Errors:2 Warnings:0)
                                            '
while($test1 =~m/ ^        Error.*\\r\\n/) print $1;
0
 
TolgarAuthor Commented:
This is what I get:  

DB<3> x @errors
0  'Errors:2 Warnings:0)'

Open in new window

0
 
TolgarAuthor Commented:
ok. Let me divide the problem.

How can I capture a string which starts with Error and end with Line: XX

XX is any number.

Example string:

        Error: Unable to include file 'iostream'File:  /dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/test_5.cpp Line: 13

Open in new window

0
 
farzanjCommented:
Well my code works and I tested except that it had errors instead of error.

#!/usr/bin/perl

$/ = undef;
$" = "\n";

my $str = <>;
my (@errors) = $str =~ /(Error:[^\n\r]+)/g;
($numErrors) = $str =~ /\(Errors:(\d+)/;

print "@errors\n";
print $numErrors,"\n";

Open in new window


usage: test.pl datafile
0
 
TolgarAuthor Commented:
@errors returns the following for my case:

  DB<4> x @errors
0  'Errors:0 Warnings:172)'
  DB<5> 

Open in new window



Apparently, the string is not one line string.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now