Simple Regular Expression.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EFP

    Simple Regular Expression.

    Can anyone help me with a simple regular expression problem.

    All that I want to do is take a list of known data and extract a
    particular section of the string to form a new list.

    Here is my code snipet:

    my $fh = new FileHandle('c:\ Units.txt') ; ## sucks up my work file
    my @lines1 = <$fh>; ## assigns the filehandler contents to @lines1
    foreach $string (@lines1) ## extract each line from @lines and put it
    in $string
    {

    $string =~ m/\*@@\$/;

    print "NewString :: $string\n";
    }

    Sample Units.txt data:
    ..\Source\CPP\C onnPool\src\ora Utils.pc@@
    ..\Source\CPP\C onnPool\src\sam ple.pc@@
    ..\Source\CPP\S ervices\src\OEx ceptServer.pc@@
    ..\Source\CPP\S ervices\src\Ree s1440Server.pc@ @
    ..\Source\CPP\S ervices\src\SWD TestServer.pc@@
    ..\Source\CPP\T estCases\TestRe es1800\testRees 1800Cursor.pc@@
    ..\Source\CPP\T SRI\Remis1\cxxs rc\Buffer_Area. pc@@

    I just want the:
    oraUtils.pc

    I'd even settle for:
    oraUtils.pc@@

    So basically I want the contents between "\" and the "@@"

    I thought that:
    $string =~ m/\*@@\$/;

    Would go to the end of the search line and find "@@" and get anything
    "*" between the "@@" and the "\" and set $string to this new value.

    What it returns is:
    element:: .\Source\CPP\Co nnPool\src\oraU tils.pc@@

    Can anyone correct my understanding of regular expression and
    assignment to a varibale.

    Thanks.
    EFP
  • Gunnar Hjalmarsson

    #2
    Re: Simple Regular Expression.

    EFP wrote:[color=blue]
    > Sample Units.txt data:
    > .\Source\CPP\Co nnPool\src\oraU tils.pc@@[/color]

    <snip>
    [color=blue]
    > I just want the:
    > oraUtils.pc[/color]

    <snip>
    [color=blue]
    > So basically I want the contents between "\" and the "@@"
    >
    > I thought that:
    > $string =~ m/\*@@\$/;
    >
    > Would go to the end of the search line and find "@@" and get
    > anything "*" between the "@@" and the "\" and set $string to this
    > new value.[/color]

    What on earth made you think that??
    [color=blue]
    > Can anyone correct my understanding of regular expression and
    > assignment to a varibale.[/color]

    Yes, you can do that. By studying the Perl documentation for regular
    expressions:



    This would do what you want:

    ($string) = $string =~ /.*\\(.+)\@\@/;

    But please use the docs to get an understanding of how it works.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • EFP

      #3
      Re: Simple Regular Expression.

      Based upon your expression I'm still not sure how it works. You have
      the first ".*" which says 0 or more periods. Then you escape the slash
      to look for a "\" then the "(.+)" match 1 or more periods and then you
      escape both @@'s individually. So I still don't understand how the
      ".*" and the "(.+)" are used. Also, how does this say give me the
      contents between the \ and the @@. I have read the reference pages
      before and after this post. Could you shed some light on the
      reasoning? I'm sure once you explain it regular expression will be
      easier to comprehend.

      ..\Source\CPP\C onnPool\src\ora Utils.pc@@ (source)

      Thanks




      Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<3S4xb.394 20$dP1.146947@n ewsc.telia.net> ...[color=blue]
      > EFP wrote:[color=green]
      > > Sample Units.txt data:
      > > .\Source\CPP\Co nnPool\src\oraU tils.pc@@[/color]
      >
      > <snip>
      >[color=green]
      > > I just want the:
      > > oraUtils.pc[/color]
      >
      > <snip>
      >[color=green]
      > > So basically I want the contents between "\" and the "@@"
      > >
      > > I thought that:
      > > $string =~ m/\*@@\$/;
      > >
      > > Would go to the end of the search line and find "@@" and get
      > > anything "*" between the "@@" and the "\" and set $string to this
      > > new value.[/color]
      >
      > What on earth made you think that??
      >[color=green]
      > > Can anyone correct my understanding of regular expression and
      > > assignment to a varibale.[/color]
      >
      > Yes, you can do that. By studying the Perl documentation for regular
      > expressions:
      >
      > http://www.perldoc.com/perl5.8.0/pod/perlre.html
      >
      > This would do what you want:
      >
      > ($string) = $string =~ /.*\\(.+)\@\@/;
      >
      > But please use the docs to get an understanding of how it works.[/color]

      Comment

      • Gunnar Hjalmarsson

        #4
        Re: Simple Regular Expression.

        EFP wrote:[color=blue]
        > Gunnar Hjalmarsson wrote:[color=green]
        >> EFP wrote:[color=darkred]
        >>>
        >>> Sample Units.txt data:
        >>> .\Source\CPP\Co nnPool\src\oraU tils.pc@@
        >>>
        >>> I just want the:
        >>> oraUtils.pc[/color]
        >>
        >> ($string) = $string =~ /.*\\(.+)\@\@/;[/color]
        >
        > Based upon your expression I'm still not sure how it works. You
        > have the first ".*" which says 0 or more periods.[/color]

        No it doesn't. '.' is a regex metacharacter that matches any character
        (except newline) if it's not escaped, so it says 0 or more of _any_
        characters.
        [color=blue]
        > Then you escape the slash to look for a "\"[/color]

        Correct. Since the '.*' is "greedy", the '\\' matches the _last_
        backslash. (Read about "greediness " in perldoc perlre.)
        [color=blue]
        > then the "(.+)" match 1 or more periods[/color]

        Again, it matches 1 or more of _any_ characters. The parentheses
        capture the content, and the regex returns it when it's evaluated in
        list context. The parentheses surrounding $string enforces list
        context. (Read about "context" in perldoc perldata.)
        [color=blue]
        > and then you escape both @@'s individually.[/color]

        Yep.

        --
        Gunnar Hjalmarsson
        Email: http://www.gunnar.cc/cgi-bin/contact.pl

        Comment

        Working...