Loading ...
Sorry, an error occurred while loading the content.

Regular expressions help

Expand Messages
  • nikhil deshpande
    Hi everyone, I am preparing a script to extract certain data from a file. The file is stored at the location specified in the variable $dir_path below. There
    Message 1 of 4 , Jul 29, 2009
    • 0 Attachment
      Hi everyone,

      I am preparing a script to extract certain data from a file. The file is stored at the location specified
      in the variable $dir_path below. There are several such files and the data is extracted automatically as the
      script runs.
      For that purpose, I am facing following problem:

      Following is the path to the file:

      $dir_path = </abc/def/ghi/pp123456.2.3.4.jk.xyz>;


      Now for a separate part of the same script, I want to extract only the following part of
      the variable $dir_path and save it in some other variable:

      pp123456.2.3.4.jk.xyz

      I don't know exactly how to get only that part of the variable $dir_path. Remember the
      filename is not a constant. It will change every time the script runs depending on what
      data needs to be extracted.

      Waiting for your reply,
      thanks in advance,
      Nik


      Looking for local information? Find it on Yahoo! Local http://in.local.yahoo.com/

      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... nikhil $dir_path = ; What is this? Not Perl. Some other language? -- Randal L. Schwartz - Stonehenge Consulting
      Message 2 of 4 , Jul 29, 2009
      • 0 Attachment
        >>>>> "nikhil" == nikhil deshpande <deshpandenikhils@...> writes:

        nikhil> $dir_path = </abc/def/ghi/pp123456.2.3.4.jk.xyz>;

        What is this? Not Perl.

        Some other language?

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      • Jeff Soules
        On Wed, Jul 29, 2009 at 6:45 PM, nikhil ... Generally, the best practice is to use well-tested and thoroughly-designed modules to solve problems like these.
        Message 3 of 4 , Jul 29, 2009
        • 0 Attachment
          On Wed, Jul 29, 2009 at 6:45 PM, nikhil
          deshpande<deshpandenikhils@...> wrote:
          > Now for a separate part of the same script, I want to extract only the
          > following part of the variable $dir_path and save it in some other variable:

          Generally, the best practice is to use well-tested and
          thoroughly-designed modules to solve problems like these. For sure
          you could come up with a regular expression that ought to mostly work,
          but then you'll come across an edge case that you weren't expecting,
          and things will break.

          For instance, for this problem, I would recommend you look at the
          File::Basename[1] package, and possibly other packages available on
          search.cpan.org.

          Get to know and love CPAN. Many people have come before you, and left
          a lot of code to make your way smoother and clearer. It's only polite
          to try to use what they've contributed : )


          Good luck,
          Jeff


          [1] http://search.cpan.org/~tty/kurila-1.19_0/lib/File/Basename.pm
        • Vivek Kumar
          Hi Nikhil, hope this will resolve your problem $dir_path = /abc/def/ghi/pp123456.2.3.4.jk.xyz ; $filename = ($dir_path =~ /(.*) /(.*) /(.*) /(.*)/); $filename
          Message 4 of 4 , Aug 1, 2009
          • 0 Attachment
            Hi Nikhil,

            hope this will resolve your problem

            $dir_path = "/abc/def/ghi/pp123456.2.3.4.jk.xyz";
            $filename = ($dir_path =~ /(.*)\/(.*)\/(.*)\/(.*)/);
            $filename = $4;
            $fileDirectory = $1."/"."$2"."/".$3;
            print $filename;
            print "\n";
            print $fileDirectory;

            The output of the result will be :

            pp123456.2.3.4.jk.xyz
            /abc/def/ghi

            Regards,
            Vivek Kumar




            --- In perl-beginner@yahoogroups.com, nikhil deshpande <deshpandenikhils@...> wrote:
            >
            > Hi everyone,
            >
            > I am preparing a script to extract certain data from a file. The file is stored at the location specified
            > in the variable $dir_path below. There are several such files and the data is extracted automatically as the
            > script runs.
            > For that purpose, I am facing following problem:
            >
            > Following is the path to the file:
            >
            > $dir_path = </abc/def/ghi/pp123456.2.3.4.jk.xyz>;
            >
            >
            > Now for a separate part of the same script, I want to extract only the following part of
            > the variable $dir_path and save it in some other variable:
            >
            > pp123456.2.3.4.jk.xyz
            >
            > I don't know exactly how to get only that part of the variable $dir_path. Remember the
            > filename is not a constant. It will change every time the script runs depending on what
            > data needs to be extracted.
            >
            > Waiting for your reply,
            > thanks in advance,
            > Nik
            >
            >
            > Looking for local information? Find it on Yahoo! Local http://in.local.yahoo.com/
            >
            > [Non-text portions of this message have been removed]
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.