Author |
Message |
piet #1 / 10
|
 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 |
|
 |
John W. Krah #2 / 10
|
 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 |
|
 |
Michael Budas #3 / 10
|
 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 |
|
 |
Benjamin Goldber #4 / 10
|
 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 |
|
 |
Barry Kimelma #5 / 10
|
 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 |
|
 |
piet #6 / 10
|
 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 |
|
 |
John W. Krah #7 / 10
|
 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 |
|
 |
piet #8 / 10
|
 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 |
|
 |
Anno Sieg #9 / 10
|
 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 |
|
 |
Tad McClell #10 / 10
|
 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 |
|
|