Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > search substring in a large string

Reply
Thread Tools

search substring in a large string

 
 
Paul
Guest
Posts: n/a
 
      12-09-2005
hi, greetings,

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


$mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwer abcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245 234";

$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.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-09-2005
Paul wrote:
> I want to get the substring with the same prefix in a large string
> (about 6K size ). like this,
>
>
> $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwer abcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245 234";
>
> $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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-09-2005
Paul wrote:
> I want to get the substring with the same prefix in a large string
> (about 6K size ). like this,
>
> $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwer abcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245 234";
>
> $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;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-09-2005
Paul <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> hi, greetings,
>
> I want to get the substring with the same prefix in a large string
> (about 6K size ). like this,
>
>
> $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwer abcdefgh12345

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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-10-2005
Anno Siegel wrote:
> Paul <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >
> > $substring=substr($mystring,$pos1,16);
> >
> > In this way, I can get the first 16bit substring after "abcdefgh", the

>
> You mean "16-byte substring".


You mean "16-character substring"

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

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-10-2005

Paul wrote:
> hi, greetings,
>
> I want to get the substring with the same prefix in a large string
> (about 6K size ). like this,
> $mystring="adfadfabcdefg234q34qwerqwerqewrqwerqwer abcdefgh12345dafsdfe4q35345abcdefghhherwrtwert3245 234";
>
> $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;

}

 
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
Help with regex and optional substring in search string Timur Tabi Python 4 10-14-2009 10:22 PM
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
Search substring in a string and get index of all occurances Nico Grubert Python 1 06-21-2006 08:35 AM
Substring search in a sorted Map kirthiganarayanan@yahoo.com Java 3 12-27-2005 07:09 AM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM



Advertisments