search substring in a large string

P

Paul

hi, greetings,

I want to get the substring with the same prefix in a large string
(about 6K size ). like this,


$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";

$pos1=index($mystring,"abcdefgh");

$substring=substr($mystring,$pos1,16);


In this way, I can get the first 16bit substring after "abcdefgh", the
questions is How can I get the 2nd, 3rd substring after "abcdefgh"?


thanks.
 
J

Jürgen Exner

Paul said:
I want to get the substring with the same prefix in a large string
(about 6K size ). like this,


$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";

$pos1=index($mystring,"abcdefgh");

$substring=substr($mystring,$pos1,16);


In this way, I can get the first 16bit substring

What is a 16bit substring?
after "abcdefgh", the
questions is How can I get the 2nd, 3rd substring after "abcdefgh"?

Just an idea: why not split() the string at 'abcdefgh' an then grab whatever
you need from each element of the returned list?

jue
 
G

Gunnar Hjalmarsson

Paul said:
I want to get the substring with the same prefix in a large string
(about 6K size ). like this,

$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";

$pos1=index($mystring,"abcdefgh");

$substring=substr($mystring,$pos1,16);

In this way, I can get the first 16bit substring after "abcdefgh",

Suppose you mean the first 16 characters substring beginning with
"abcdefgh".
How can I get the 2nd, 3rd substring after "abcdefgh"?

my ($pos, @substrings) = 0;
while ( ( $pos = index $mystring, 'abcdefgh', $pos ) >= 0 ) {
push @substrings, substr($mystring, $pos, 16);
$pos += 16;
}
 
A

Anno Siegel

Paul said:
hi, greetings,

I want to get the substring with the same prefix in a large string
(about 6K size ). like this,


$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345 dafsdfe4q35345abcdefghhherwrtwert3245234";

$pos1=index($mystring,"abcdefgh");

$substring=substr($mystring,$pos1,16);


In this way, I can get the first 16bit substring after "abcdefgh", the

You mean "16-byte substring".
questions is How can I get the 2nd, 3rd substring after "abcdefgh"?

Read the documentation of index again and pay attention to the parameter
named "POSITION".

Or get them all at once:

my @extract = $mystring =~ /abcdefgh(.{16})/sg;

Anno
 
B

Brian McCauley

Anno said:
You mean "16-byte substring".

You mean "16-character substring" :)

substr() operates on characters not bytes unless the "use bytes" pragma
is in effect.
 
B

Brian McCauley

Paul said:
hi, greetings,

I want to get the substring with the same prefix in a large string
(about 6K size ). like this,
$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwerabcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245234";

$pos1=index($mystring,"abcdefgh");

$substring=substr($mystring,$pos1,16);

In this way, I can get the first 16bit substring after "abcdefgh", the
questions is How can I get the 2nd, 3rd substring after "abcdefgh"?

Although index()/substr() are often faster at runtime it may be more
ideomatic to use a regex match.

If you perfer to optomise for readability over speed and want to loop
through all matches...

while ( $mystring =~ /(abcdefgh.{8})/g ) {
my $substring = $1;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top