extract string from another string 
Author Message
 extract string from another string

$teststring = 'stuff 650 N other stuff'
another example of string 'stuff stuff stuff stuff 90 N'

What would I do to extract 650 N and 90 N from the string?

650 N is an example: it can be from 111 A to 999 Z
90 N is an example: it can be from 10 A to 99 N
They are not on a specific place in the string.

I know how to check if they are in the string, but I don't now how to
extract them. So I need 650 N and 90 N stored in a variable or an array

This is what I already have:
if ($teststring =~ /\d\d\d [a-zA-Z]|\d\d [a-zA-Z]/) {
print $teststring;

Quote:
}

Thanks for your help
piet


Thu, 11 Aug 2005 23:35:07 GMT  
 extract string from another string

Quote:

> $teststring = 'stuff 650 N other stuff'
> another example of string 'stuff stuff stuff stuff 90 N'

> What would I do to extract 650 N and 90 N from the string?

> 650 N is an example: it can be from 111 A to 999 Z
> 90 N is an example: it can be from 10 A to 99 N
> They are not on a specific place in the string.

> I know how to check if they are in the string, but I don't now how to
> extract them. So I need 650 N and 90 N stored in a variable or an array

> This is what I already have:
> if ($teststring =~ /\d\d\d [a-zA-Z]|\d\d [a-zA-Z]/) {
> print $teststring;
> }

my ($extract) = $teststring =~ /\b(\d{2,3} [a-zA-Z])\b/;

John
--
use Perl;
program
fulfillment



Thu, 11 Aug 2005 23:50:02 GMT  
 extract string from another string

Quote:

> $teststring = 'stuff 650 N other stuff'
> another example of string 'stuff stuff stuff stuff 90 N'

> What would I do to extract 650 N and 90 N from the string?

> 650 N is an example: it can be from 111 A to 999 Z
> 90 N is an example: it can be from 10 A to 99 N
> They are not on a specific place in the string.

> I know how to check if they are in the string, but I don't now how to
> extract them. So I need 650 N and 90 N stored in a variable or an array

> This is what I already have:
> if ($teststring =~ /\d\d\d [a-zA-Z]|\d\d [a-zA-Z]/) {
> print $teststring;
> }

> Thanks for your help
> piet

close:

if ($teststring =~ /(\d{3} [a-z]|\d{2} [a-n])/i) {
  print $1;

Quote:
}

notice the capturing parens...

hth-



Fri, 12 Aug 2005 03:24:02 GMT  
 extract string from another string
[snip]

Quote:
> This is what I already have:
> if ($teststring =~ /\d\d\d [a-zA-Z]|\d\d [a-zA-Z]/) {
> print $teststring;
> }

   my ($extracted) = $teststring =~ /(\d\d\d? [a-z])/i;

--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print



Fri, 12 Aug 2005 03:51:50 GMT  
 extract string from another string


Quote:
> $teststring = 'stuff 650 N other stuff'
> another example of string 'stuff stuff stuff stuff 90 N'

> What would I do to extract 650 N and 90 N from the string?

> 650 N is an example: it can be from 111 A to 999 Z
> 90 N is an example: it can be from 10 A to 99 N
> They are not on a specific place in the string.

> I know how to check if they are in the string, but I don't now how to
> extract them. So I need 650 N and 90 N stored in a variable or an array

> This is what I already have:
> if ($teststring =~ /\d\d\d [a-zA-Z]|\d\d [a-zA-Z]/) {
> print $teststring;
> }

> Thanks for your help
> piet

if ($teststring =~ /(\d\d\d [a-zA-Z]|\d\d [a-zA-Z])/) {
    print "Found a match in : $teststing\n";
    print "The part I matched was [$1]\n";

Quote:
}

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada



Fri, 12 Aug 2005 15:18:57 GMT  
 extract string from another string

Quote:


> > $teststring = 'stuff 650 N other stuff'
> > another example of string 'stuff stuff stuff stuff 90 N'

Thank you all for your replies.

what it there were several (from 1 to 10) matches to extract?
example:
$teststring =' stuff 650 N other stuff 90N stuff"

Txs
Piet



Fri, 12 Aug 2005 21:48:58 GMT  
 extract string from another string

Quote:

> what it there were several (from 1 to 10) matches to extract?
> example:
> $teststring =' stuff 650 N other stuff 90N stuff"


John
--
use Perl;
program
fulfillment



Sat, 13 Aug 2005 02:15:21 GMT  
 extract string from another string

Quote:

> > $teststring =' stuff 650 N other stuff 90N stuff"


> John

Thanks again John!

Where coudl I find all these switches (\b /g *) explained on internet?
I did a search on google but did not find anything.

Piet



Sat, 13 Aug 2005 10:46:23 GMT  
 extract string from another string

Quote:

> > > $teststring =' stuff 650 N other stuff 90N stuff"


> > John

> Thanks again John!

> Where coudl I find all these switches (\b /g *) explained on internet?

No need for the Internet.  If you have Perl on your computer you
have its complete documentation.  Type "perldoc perlre" at a
command prompt for regular expressions.  For a general introduction,
start with "perldoc perldoc" and "perldoc perl".

Anno



Sat, 13 Aug 2005 10:53:13 GMT  
 extract string from another string

Quote:



> Where coudl I find all these switches (\b /g *) explained on internet?

They are not switches.

You don't need the internet, the documentation for Perl comes
with perl, so it is already on your hard disk somewhere.

You need to read up on two different things, regular expressions
(eg. \b and *) and operators that use regular expressions (eg. m//g).

   perldoc perlrequick
   perldoc perlretut
   perldoc perlre

   perldoc perlop

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 13 Aug 2005 14:41:42 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Extract string from a string

2. regular expression to extract a string between 2 other strings

3. Extracting strings from postscript in a single regexp?

4. Extracting strings with Perl.

5. extracting error strings from C source

6. Extracting Specific Chars from a Whole Large String

7. How to extract the toplevel-domain of a domainame from a string with regexp

8. Extract a substring in middle of a string.

9. How do you extract bits from a string?

10. How can I extract C'string

11. extracting strings from c-source ?

12. : Extracting matches from a string

 

 
Powered by phpBB® Forum Software