zhihua li wrote: > hi netters, > > I'm curious if there's any smart code to calculate the "distance" > between the matches in a text. > Suppose I have a text like this: > syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes... > Apparently it has multiple words of "yes". I'd like to know how many > characters there are between the first and the last "yes". The way I now > come up with to do this is to use substitution to "mark" the first and > the last match, then use a counting loop to calculate the > characters......rather straightforward and stupid method. > > Anyone has a better idea about that? > > Thanks a lot!
Hi. If you use a regex to capture the characters between the yeses you can just take the length of the capture: my $text = q(syhk...yes...uhg;ka=...yes...yiealg.....yes.......ghe;a...yes...); $text =~ /yes(.*)yes/; my $nchars = length $1; print $nchars, "\n"; The answer's 48! HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>