Simple Regular Expression.

Discussion in 'Perl' started by EFP, Nov 26, 2003.

  1. EFP

    EFP Guest

    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\ConnPool\src\oraUtils.pc@@
    ..\Source\CPP\ConnPool\src\sample.pc@@
    ..\Source\CPP\Services\src\OExceptServer.pc@@
    ..\Source\CPP\Services\src\Rees1440Server.pc@@
    ..\Source\CPP\Services\src\SWDTestServer.pc@@
    ..\Source\CPP\TestCases\TestRees1800\testRees1800Cursor.pc@@
    ..\Source\CPP\TSRI\Remis1\cxxsrc\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\ConnPool\src\oraUtils.pc@@

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

    Thanks.
    EFP
    EFP, Nov 26, 2003
    #1
    1. Advertising

  2. EFP wrote:
    > Sample Units.txt data:
    > .\Source\CPP\ConnPool\src\oraUtils.pc@@


    <snip>

    > I just want the:
    > oraUtils.pc


    <snip>

    > 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 on earth made you think that??

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


    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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 26, 2003
    #2
    1. Advertising

  3. EFP

    EFP Guest

    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\ConnPool\src\oraUtils.pc@@ (source)

    Thanks




    Gunnar Hjalmarsson <> wrote in message news:<3S4xb.39420$>...
    > EFP wrote:
    > > Sample Units.txt data:
    > > .\Source\CPP\ConnPool\src\oraUtils.pc@@

    >
    > <snip>
    >
    > > I just want the:
    > > oraUtils.pc

    >
    > <snip>
    >
    > > 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 on earth made you think that??
    >
    > > Can anyone correct my understanding of regular expression and
    > > assignment to a varibale.

    >
    > 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.
    EFP, Dec 1, 2003
    #3
  4. EFP wrote:
    > Gunnar Hjalmarsson wrote:
    >> EFP wrote:
    >>>
    >>> Sample Units.txt data:
    >>> .\Source\CPP\ConnPool\src\oraUtils.pc@@
    >>>
    >>> I just want the:
    >>> oraUtils.pc

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

    >
    > Based upon your expression I'm still not sure how it works. You
    > have the first ".*" which says 0 or more periods.


    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.

    > Then you escape the slash to look for a "\"


    Correct. Since the '.*' is "greedy", the '\\' matches the _last_
    backslash. (Read about "greediness" in perldoc perlre.)

    > then the "(.+)" match 1 or more periods


    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.)

    > and then you escape both @@'s individually.


    Yep.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 1, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,279
  2. Q. John Chen

    Simple Regular Expression need

    Q. John Chen, Aug 27, 2005, in forum: ASP .Net
    Replies:
    18
    Views:
    7,717
    Lucas Tam
    Aug 31, 2005
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    838
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,940
    vforvikash
    Dec 29, 2008
  5. Joey
    Replies:
    7
    Views:
    464
    Kevin Spencer
    May 24, 2006
Loading...

Share This Page