search "window" pattern matching

C

Cheez

gnari said:
minor nitpick #2:
what you did here is called top-posting: you made a follow-up,
and quoted the message you are following-up on below.
this practice is frowned-upon in this newsgroup.
this case it is not serious, because you did not actually quote the whole
article below.


My bad. Didn't notice the convention here at c.l.p.m It's different
in different places. Thanks for the tip. -Cheez
 
B

Becky Alcorn

Cheez said:
$mystring = "thetextinherewillbefairlyrandom";

I want to capture chunks of text and place them in an array or hash
table. If possible, I want to make a regex that will start at the
first letter and capture letters 1 - 5, in this case $capture =
"thete". Then, I want this window to shift 1 letter so that the next
captured string is letters 2 - 6, or $capture= "hetex" and so on until
the end of the line. Can anyone offer up a sample regex would
accomplish this task?

Unpack might be simpler. Try:

use strict;

my $text = "thetextinherewillbefairlyrandom";
my $count = 0;
my @result;

while (my $matched = unpack("x${count}A5", $text)) {
push (@result, $matched);
$count++;
}
 
A

Anno Siegel

Becky Alcorn said:
Unpack might be simpler. Try:

use strict;

my $text = "thetextinherewillbefairlyrandom";
my $count = 0;
my @result;

while (my $matched = unpack("x${count}A5", $text)) {
push (@result, $matched);
$count++;
}

Not quite. We have so far assumed the OP wants only the substrings of
length 5. Yours delivers a number of shorter strings at the end.

@result = map unpack("x${_}A5", $text), 1 .. length( $text) - 5;

Anno
 
J

John W. Krahn

Anno said:
Not quite. We have so far assumed the OP wants only the substrings of
length 5. Yours delivers a number of shorter strings at the end.

@result = map unpack("x${_}A5", $text), 1 .. length( $text) - 5;

Also, using the 'A' format for unpack means that if there is any
whitespace in the original string then not all of the substrings will be
five characters in length. You should use the 'a' format instead.


John
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top