Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie: How to I extract word

Reply
Thread Tools

Newbie: How to I extract word

 
 
Mav
Guest
Posts: n/a
 
      07-01-2004
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
 
Reply With Quote
 
 
 
 
GM
Guest
Posts: n/a
 
      07-01-2004
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
 
Reply With Quote
 
 
 
 
Dale Henderson
Guest
Posts: n/a
 
      07-01-2004
>>>>> "GM" == GM <(E-Mail Removed)> 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
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      07-02-2004
Dale Henderson wrote:
>>>>>>"GM" == GM <(E-Mail Removed)> 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

 
Reply With Quote
 
Mav
Guest
Posts: n/a
 
      07-02-2004
Josef Moellers <(E-Mail Removed)> wrote in message news:<cc308c$1nt$(E-Mail Removed)-siemens.com>...
> Dale Henderson wrote:
> >>>>>>"GM" == GM <(E-Mail Removed)> 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
 
Reply With Quote
 
Dale Henderson
Guest
Posts: n/a
 
      07-03-2004
>>>>> "JM" == Josef Moellers <(E-Mail Removed)> 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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-05-2004
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
 
Reply With Quote
 
Dale Henderson
Guest
Posts: n/a
 
      07-05-2004
>>>>> "JS" == Joe Smith <(E-Mail Removed)> 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
 
Reply With Quote
 
Mav
Guest
Posts: n/a
 
      07-07-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-commerce.edu>...
> >>>>> "JS" == Joe Smith <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
How to extract part of the text (htm) file after start word until end word? Perl Misc 4 05-12-2006 06:51 PM
how i can extract text from the PDF files,power point files,Ms word files? crazyprakash Java 4 10-30-2005 10:17 AM
Processing content (extract cross-references) of MS Word questioner Python 0 05-04-2004 11:34 AM



Advertisments