extracting a string from between parens

extracting a string from between parens

Post by Bob Showalt » Fri, 28 Jun 2002 00:38:14



> -----Original Message-----

> Sent: Wednesday, June 26, 2002 11:30 AM

> Subject: RE: extracting a string from between parens

> >I have this situation:
> >       $in = "02 Jul 5.00 (YHZ GA-E)"

> >I want:
> >       $out = "YHZGA-E"

> >How do I extract the information between the ()s ?
> >Other than the parens all other characters are likely to change.

> Perhaps
> $in =~ /\((.*?)\)/;
> $out = $1;

This is a problem if there is no match. $1 will have whatever it's
previous value was. Just use match operator in list context:

  ($out) = $in =~ /\((.*?)\)/;

Now $out will be undef if there is no match.

 
 
 

extracting a string from between parens

Post by Todd Shiffle » Fri, 28 Jun 2002 00:26:17


I have this situation:
        $in = "02 Jul 5.00 (YHZ GA-E)"

I want:
        $out = "YHZGA-E"

How do I extract the information between the ()s ?
Other than the parens all other characters are likely to change.

-todd

 
 
 

extracting a string from between parens

Post by Shishir K. Sin » Fri, 28 Jun 2002 00:30:10


Quote:>I have this situation:
>    $in = "02 Jul 5.00 (YHZ GA-E)"
>I want:
>    $out = "YHZGA-E"
>How do I extract the information between the ()s ?
>Other than the parens all other characters are likely to change.

Perhaps
$in =~ /\((.*?)\)/;
$out = $1;

Should work!!

-todd

--


 
 
 

extracting a string from between parens

Post by Shishir K. Sin » Fri, 28 Jun 2002 00:43:13


> -----Original Message-----

> Sent: Wednesday, June 26, 2002 11:30 AM

> Subject: RE: extracting a string from between parens

> >I have this situation:
> >       $in = "02 Jul 5.00 (YHZ GA-E)"

> >I want:
> >       $out = "YHZGA-E"

> >How do I extract the information between the ()s ?
> >Other than the parens all other characters are likely to change.

> Perhaps
> $in =~ /\((.*?)\)/;
> $out = $1;
>This is a problem if there is no match. $1 will have whatever it's
>previous value was. Just use match operator in list context:
>  ($out) = $in =~ /\((.*?)\)/;
>Now $out will be undef if there is no match.

or

$out = $1 if ($in =~ /\((.*?)\)/);

 
 
 

extracting a string from between parens

Post by Shishir K. Sin » Fri, 28 Jun 2002 00:48:57


Quote:

> >I have this situation:
> >       $in = "02 Jul 5.00 (YHZ GA-E)"

> >I want:
> >       $out = "YHZGA-E"

> >How do I extract the information between the ()s ?
> >Other than the parens all other characters are likely to change.

> Perhaps
> $in =~ /\((.*?)\)/;
> $out = $1;
>This is a problem if there is no match. $1 will have whatever it's
>previous value was. Just use match operator in list context:
>  ($out) = $in =~ /\((.*?)\)/;
>Now $out will be undef if there is no match.
>or
>$out = $1 if ($in =~ /\((.*?)\)/);

Nope..won't work..I take it back!!

--


 
 
 

extracting a string from between parens

Post by Bob Ackerm » Fri, 28 Jun 2002 01:07:21



>> $out = $1 if ($in =~ /\((.*?)\)/);
> Nope..won't work..I take it back!!

looks ok to me. except it adds extra strokes to previous answer.
what do you think is wrong with it?
 
 
 

extracting a string from between parens

Post by Mark Lob » Fri, 28 Jun 2002 01:20:19


> -----Original Message-----

> Sent: Wednesday, June 26, 2002 9:07 AM

> Subject: Re: extracting a string from between parens


> >> $out = $1 if ($in =~ /\((.*?)\)/);
> > Nope..won't work..I take it back!!

> looks ok to me. except it adds extra strokes to previous answer.
> what do you think is wrong with it?

Perhaps it is that $out will still be the previous value if no match?
 
 
 

extracting a string from between parens

Post by Shishir K. Sin » Fri, 28 Jun 2002 01:26:14



>> $out = $1 if ($in =~ /\((.*?)\)/);
> Nope..won't work..I take it back!!

>looks ok to me. except it adds extra strokes to previous answer.
>what do you think is wrong with it?

It will work fine as far as the regex is concerned. I think what Todd may want is to check the $out for defined or undefined. This line won't give him the same kind of validation.

Perhaps..he should be doing it this way

$in =~ /\((.*?)\)/;
$out = (defined($1) ? $1 : "default/undef/whatever??";

 
 
 

extracting a string from between parens

Post by Bob Showalt » Fri, 28 Jun 2002 01:36:55


> -----Original Message-----

> Sent: Wednesday, June 26, 2002 12:26 PM

> Subject: RE: extracting a string from between parens


> >> $out = $1 if ($in =~ /\((.*?)\)/);
> > Nope..won't work..I take it back!!

> >looks ok to me. except it adds extra strokes to previous answer.
> >what do you think is wrong with it?

> It will work fine as far as the regex is concerned. I think
> what Todd may want is to check the $out for defined or
> undefined. This line won't give him the same kind of validation.

> Perhaps..he should be doing it this way

> $in =~ /\((.*?)\)/;
> $out = (defined($1) ? $1 : "default/undef/whatever??";

Sorry, still not right. defined($1) is no better than $1 alone after
a failed match.

Again, the most straightforward solution is:

  ($out) = $in =~ /\((.*?)\)/;

 
 
 

extracting a string from between parens

Post by Shishir K. Sin » Fri, 28 Jun 2002 01:42:37



> >> $out = $1 if ($in =~ /\((.*?)\)/);
> > Nope..won't work..I take it back!!

> >looks ok to me. except it adds extra strokes to previous answer.
> >what do you think is wrong with it?

> It will work fine as far as the regex is concerned. I think
> what Todd may want is to check the $out for defined or
> undefined. This line won't give him the same kind of validation.

> Perhaps..he should be doing it this way

> $in =~ /\((.*?)\)/;
> $out = (defined($1) ? $1 : "default/undef/whatever??";
>Sorry, still not right. defined($1) is no better than $1 alone after
>a failed match.
>Again, the most straightforward solution is:
>  ($out) = $in =~ /\((.*?)\)/;

You are right ...this will be the the most simple sol..in case nothing needs to be done to $out...but in case
some thing needs to be done on $out which I am assuming to be true, then the value of $out needs to be checked again,  which introduces additional lines and variable declaration.
 
 
 

extracting a string from between parens

Post by Drie » Fri, 28 Jun 2002 05:33:30



> I have this situation:
>    $in = "02 Jul 5.00 (YHZ GA-E)"

> I want:
>    $out = "YHZGA-E"

you have a two stage problem -

        a) get the stuff between the ()
        b) remove the white space
....
        if ($in =~ /\(([^\)]+)\).*/ ) {
                $out = $1;
                $out =~ s/\s*//g;
        }
....
cf:
http://www.wetware.com/drieux/pbl/RegEx/ParenPicker.txt

ciao
drieux

---

 
 
 

extracting a string from between parens

Post by David T » Fri, 28 Jun 2002 05:38:39


Shishir, et al --

...and then Shishir K. Singh said...
%
...
% >Again, the most straightforward solution is:
%
% >  ($out) = $in =~ /\((.*?)\)/;
%
% You are right ...this will be the the most simple sol..in case nothing needs to be done to $out...but in case
% some thing needs to be done on $out which I am assuming to be true, then the value of $out needs to be checked again,  which introduces additional lines and variable declaration.

Well, not if you've been a good boy and put your

  my $out

in tight scope so that it's destroyed after each check and before the
next :-)

HTH & HAND

:-D
--
David T-G                      * It's easier to fight for one's principles


http://www.justpickone.org/davidtg/    Shpx gur Pbzzhavpngvbaf Qrprapl Npg!

  application_pgp-signature_part
< 1K Download
 
 
 

1. Extract text from position x as string

Hi to all!

I want to extract text from a point  x.top, y.top  to  x.bottom, y.bottom
from a PDF-file.
(output should be ASCII or Unicode)
Does anyone knows a Java oder .NET library that can handle this? (or any
other programming-language)

Thank you very much
Thos

2. Should I buy HP 5L?

3. Extracting a String from an Any type

4. VDial updated to 3.22

5. extracting out of a string

6. Lexmark 3200

7. Regex problem extracting middle-word part of string

8. upgrade

9. extracting numbers from a string

10. Extracting bits out of a string of bytes

11. Extracting bits from a string of bits

12. need help extracting words from a string - Thanks and need more help

13. need help extracting words from a string