1

I want to find the file name with fullpath from this string

"[exec] /snschome/sns/ccyp/mb_ccsns/bb/cbil5/v85_8/amdocs/iamcust/bil/cl/business/handlers/ClReportsHandler.java:233: cannot resolve symbol"

I want to extract

/snschome/sns/ccyp/mb_ccsns/bb/cbil5/v85_8/amdocs/iamcust/bil/cl/business/handlers/ClReportsHandler.java

and I am trying this in Perl

    $_=$string_from_to_match
    my @extract_file=split(/.*(\/.*)\:.*/);
    print $extract_file[1],"\n";`

but I am getting this output:

/ClReportsHandler.java:233:

It is matching the last / and the last :. How can I change it to first / and first :?

1
  • 3
    Please edit the formatted version instead of rolling back to the unformatted one. Otherwise your question is unreadable. Commented Sep 21, 2010 at 14:29

5 Answers 5

4

This is a case where “tacking and stretching” is useful. You know that [exec] followed by whitespace is on the left and colon followed by a line number is on the right. You want what's in between:

#! /usr/bin/perl

use warnings;
use strict;

$_ = "[exec] /snschome/sns/ccyp/mb_ccsns/bb/cbil5/v85_8/amdocs/iamcust/bil/cl/business/handlers/ClReportsHandler.java:233: cannot resolve symbol";

if (/\[exec\]\s*(.+?):\d+/) {
  print $1, "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

hi it worked & from your regular expression i changed mine like this & it is working fine .. my <pre>@extract_file=split(/.*?(\/.*?)\:.*/); </pre>
1

If your file name doesn't contain spaces then you can create simple regexp to match all parts:

my ($file, $line, $msg) = ( $string_from_to_match =~ m{(\S+):([^:]+):([^:]+)} );

I've used:

  • \S+ to match 1 or more non-space symbols
  • [^:]+ to match 1 or more not : symbols

If you want spaces in path, then the best way is to remove starting [exec] part and split by ::

$string_from_to_match =~ s{^\[exec\]\s+}{};
my ($file, $line, $msg) = split(/:/, $string_from_to_match, 3);

Comments

1

You could try :

#!/usr/bin/perl
use strict;
use warnings;
use 5.10.1;

my $str = "[exec] /snschome/sns/ccyp/mb_ccsns/bb/cbil5/v85_8/amdocs/iamcust/bil/cl/business/handlers/ClReportsHandler.java:233: cannot resolve symbol";
$str =~ s!^[^/]*(/[^:]*):.*$!$1!;
say $str;

Ouput: /snschome/sns/ccyp/mb_ccsns/bb/cbil5/v85_8/amdocs/iamcust/bil/cl/business/handlers/ClReportsHandler.java

Comments

-1

Why are you using split() if you want to match?

if ($subject =~ m!(/[^:]+):!) {
    $result = $1;
} else {
    $result = "";
}

should do it.

Explanation:

!: alternative regex delimiter (since I need the / inside)

(: start capturing group

/: match a /. This will always be the first / of the string.

[^:]+: match 1 or more characters except :

): end capturing group

:: match a :

!: regex delimiter

Comments

-1

This is what I do:

if ($fullpath =~ /^(.+)\/([^\/]+)/)
{
    my ($path, $file) = ($1, $2);
}

This works by matching anything except the last /. This may not work under Windows of course.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.