Newbie: How to I extract word

Discussion in 'Perl Misc' started by Mav, Jul 1, 2004.

  1. Mav

    Mav Guest

    Hi, there

    I got string like:
    $string= "------ Build started: Project: Myproject, Config: Debug ABC
    ------";

    I would like print out only anything in between "Project:" to ",", in
    this case it is "Myproject" in perl.

    Any idea?

    Thanks,
    M
     
    Mav, Jul 1, 2004
    #1
    1. Advertising

  2. Mav

    GM Guest

    Mav wrote:
    > Hi, there
    >
    > I got string like:
    > $string= "------ Build started: Project: Myproject, Config: Debug ABC
    > ------";
    >
    > I would like print out only anything in between "Project:" to ",", in
    > this case it is "Myproject" in perl.
    >
    > Any idea?
    >
    > Thanks,
    > M


    The following assumes you have no whitespace in your project names:

    my $project = $string =~ /Project: (\S+),/;


    --

    gee DOT em ewe tee aitch AT comcast.net
     
    GM, Jul 1, 2004
    #2
    1. Advertising

  3. >>>>> "GM" == GM <> writes:

    GM> Mav wrote:
    >> Hi, there I got string like: $string= "------ Build started:
    >> Project: Myproject, Config: Debug ABC ------"; I would like
    >> print out only anything in between "Project:" to ",", in this
    >> case it is "Myproject" in perl. Any idea? Thanks, M


    GM> The following assumes you have no whitespace in your project
    GM> names:

    GM> my $project = $string =~ /Project: (\S+),/;

    The following makes no such assumption:

    my $project = $string =~ /Project: ([^,]+),/;

    :)

    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jul 1, 2004
    #3
  4. Dale Henderson wrote:
    >>>>>>"GM" == GM <> writes:

    >
    >
    > GM> Mav wrote:
    > >> Hi, there I got string like: $string= "------ Build started:
    > >> Project: Myproject, Config: Debug ABC ------"; I would like
    > >> print out only anything in between "Project:" to ",", in this
    > >> case it is "Myproject" in perl. Any idea? Thanks, M

    >
    > GM> The following assumes you have no whitespace in your project
    > GM> names:
    >
    > GM> my $project = $string =~ /Project: (\S+),/;
    >
    > The following makes no such assumption:
    >
    > my $project = $string =~ /Project: ([^,]+),/;


    Nitpicking: That's not what Mav wrote and it's not even correct:

    my $project;
    ($project = $string) =~ s/.*Project:([^,]+),.*/$1/;

    At least with Perl v5.8.1
    without the parentheses around the assignment, the value of $project is 1
    with the "my", I get "Can't declare scalar assignment in "my" at - line
    2, near ") =~""
    without the substitution, I get the entire string,
    without the .*'s, I get another string.

    You can also specify "non-greedyness":
    ($project = $string) =~ s/.*Project:(.*?),.*/$1/;

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jul 2, 2004
    #4
  5. Mav

    Mav Guest

    Josef Moellers <> wrote in message news:<cc308c$1nt$-siemens.com>...
    > Dale Henderson wrote:
    > >>>>>>"GM" == GM <> writes:

    > >
    > >
    > > GM> Mav wrote:
    > > >> Hi, there I got string like: $string= "------ Build started:
    > > >> Project: Myproject, Config: Debug ABC ------"; I would like
    > > >> print out only anything in between "Project:" to ",", in this
    > > >> case it is "Myproject" in perl. Any idea? Thanks, M

    > >
    > > GM> The following assumes you have no whitespace in your project
    > > GM> names:
    > >
    > > GM> my $project = $string =~ /Project: (\S+),/;
    > >
    > > The following makes no such assumption:
    > >
    > > my $project = $string =~ /Project: ([^,]+),/;

    >
    > Nitpicking: That's not what Mav wrote and it's not even correct:
    >
    > my $project;
    > ($project = $string) =~ s/.*Project:([^,]+),.*/$1/;
    >
    > At least with Perl v5.8.1
    > without the parentheses around the assignment, the value of $project is 1
    >
    > with the "my", I get "Can't declare scalar assignment in "my" at - line
    > 2, near ") =~""
    > without the substitution, I get the entire string,
    > without the .*'s, I get another string.
    >
    > You can also specify "non-greedyness":
    > ($project = $string) =~ s/.*Project:(.*?),.*/$1/;


    I tried it, and it seems it doesn't work, I need to do the following(silly way)

    #orgianl string
    $string= "------ Build started: Project: Myproject, Config: Debug ABC ------";

    #Get the "MyProject" index
    $w = rindex($string,",");
    $b = rindex($string,"t: ");

    if ($w > $b) {
    $projName = substr($_,$b+2,$w-$b-2); #get "Myproject" out
    print "$projName\n";
    }

    Is that a better way?
    Thanks,
    Mav
     
    Mav, Jul 2, 2004
    #5
  6. >>>>> "JM" == Josef Moellers <> writes:

    JM> Dale Henderson wrote:

    >> The following makes no such assumption:


    >> my $project = $string =~ /Project: ([^,]+),/;


    JM> Nitpicking: That's not what Mav wrote and it's not even
    JM> correct:

    What isn't what Mav wrote?

    As to the correctness, I know better than to post untested code.
    The correct version is:

    my ($project) = $string =~ /Project: ([^,]+),/;


    JM> my $project;
    JM> ($project = $string) =~s/.*Project:([^,]+),.*/$1/;

    JM> At least with Perl v5.8.1 without the parentheses around the
    JM> assignment, the value of $project is 1

    Yes. the problem is without the parenthesis, the replacement is
    being evaluated in scalar context and returning the number of
    matches. Not the first match which is what you want.

    An equivalent way to do this is:

    (my $project = $string) =~ s/.*Project:([^,]+),.*/$1/;

    However, this makes $project pointless since the replacement
    modifies $string to be the $1 and then $project is assigned the
    value of $string.

    Note also your solution leaves a leading space returning
    " Myproject" not "Myproject" which is what the OP requested but
    at the same time requested everything between "Project:" and
    ",". So we have a specification error. I suspect the OP wanted to
    eliminate leading spaces (and possibly trailing ones) which can
    be done with

    my ($project)=$string=~/Project:\s*([^,]+),/

    JM> with the "my", I get "Can't declare scalar assignment in "my"
    JM> at - line 2, near ") =~"" without the substitution, I get the
    JM> entire string,

    That's because your assigning $project to $string and ignoring the
    match.

    JM> without the .*'s, I get another string.

    You need the .*'s to delete the rest of the string. Your
    essentially replacing $string with $1.


    JM> You can also specify "non-greedyness": ($project = $string) =~
    JM> s/.*Project:(.*?),.*/$1/;

    A negated character class is the "right" answer in this case. For
    one reason its more efficient. For a discussion of why you should
    choose a negated character class over a non-greedy regex see
    "Mastering Regular Expressions" (Owl) 1st edition pgs 226-227.


    The way I would normally implement this is something like:

    my $project;

    if($string=~/Project:\s+([^,]+),/){
    $project=$1;
    }else{
    print "Bad string: $string\n"

    }

    But this may not be necessary in this case.


    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jul 3, 2004
    #6
  7. Mav

    Joe Smith Guest

    Dale Henderson wrote:

    > An equivalent way to do this is:
    >
    > (my $project = $string) =~ s/.*Project:([^,]+),.*/$1/;
    >
    > However, this makes $project pointless since the replacement
    > modifies $string to be the $1 and then $project is assigned the
    > value of $string.


    No, it does not.

    It copies the value of $string to the variable $project first, and then
    performs the substitution on $project, leaving $string untouched.

    -Joe
     
    Joe Smith, Jul 5, 2004
    #7
  8. >>>>> "JS" == Joe Smith <> writes:

    JS> Dale Henderson wrote:
    >> An equivalent way to do this is: (my $project = $string) =~
    >> s/.*Project:([^,]+),.*/$1/; However, this makes $project
    >> pointless since the replacement modifies $string to be the $1
    >> and then $project is assigned the value of $string.


    JS> No, it does not.

    JS> It copies the value of $string to the variable $project first,
    JS> and then performs the substitution on $project, leaving
    JS> $string untouched.

    Guess I should I have tested that too.

    Thanks for the correction.

    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jul 5, 2004
    #8
  9. Mav

    Mav Guest

    Thanks all, I think I should have said at the beginnig print the
    string out, instead extract..
    if ($line =~ /Project:(\s\w*)\,/) {
    print "HERE:$1\n";
    }
    Thanks all,
    Mav

    Dale Henderson <> wrote in message news:<-commerce.edu>...
    > >>>>> "JS" == Joe Smith <> writes:

    >
    > JS> Dale Henderson wrote:
    > >> An equivalent way to do this is: (my $project = $string) =~
    > >> s/.*Project:([^,]+),.*/$1/; However, this makes $project
    > >> pointless since the replacement modifies $string to be the $1
    > >> and then $project is assigned the value of $string.

    >
    > JS> No, it does not.
    >
    > JS> It copies the value of $string to the variable $project first,
    > JS> and then performs the substitution on $project, leaving
    > JS> $string untouched.
    >
    > Guess I should I have tested that too.
    >
    > Thanks for the correction.
     
    Mav, Jul 7, 2004
    #9
    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. crazyprakash
    Replies:
    4
    Views:
    3,444
    adrian
    Oct 30, 2005
  2. questioner
    Replies:
    0
    Views:
    348
    questioner
    May 4, 2004
  3. questioner

    Search and extract text in MS Word

    questioner, May 25, 2004, in forum: Python
    Replies:
    1
    Views:
    482
    Thomas Guettler
    May 25, 2004
  4. Alex Shulgin
    Replies:
    15
    Views:
    243
    Robert Klemme
    Sep 18, 2007
  5. Guest
    Replies:
    4
    Views:
    315
    Guest
    May 12, 2006
Loading...

Share This Page