Hi, On Thu, 2011-08-25 at 11:35 +0200, Paul Johnson wrote: > > > On Thu, 25 Aug 2011 10:42:20 +0200 > > > Honza Mach <jan.m...@cesnet.cz> wrote: > > > > > > > Hi everybody, > > > > > > > > I was wondering, if it is possible to use backreferences in the pattern > > > > repetition bracket operator. > > > > > > > > Consider the following string: > > > > > > > > my $string = "5 abcdefghijklmn"; > > > > > > > > The number five at the beginning of the string means, that I want to > > > > extract first five characters from the latter part of the string. I > > > > tried the following code, but it doesn`t work: > > > > > > > > $string =~ s/(\d+)\s+(.{\g1})//; > > > > print "extracted: $1 $2\n"; > > > > > > > > The desired output would be: > > > > > > > > extracted: 5 abcde > > Something like this will work: > > say "extracted: ", /^(\d+\s+.{$len})/ if ($len) = /^(\d+)/ > Sometimes two passes are are better than one. >
Thank you for your advice, however I wanted to do it in one pass because of the following reasons: 1. I wanted to compile the pattern because of the performance (I guess it is not possible to compile the pattern, when there are some substitutions of the variables from outside of the RE engine, or am I wrong?) 2. Perhaps I should have mentioned, that the application is parsing the continuos string stream coming from a socket, there are many of these "commands" coming one after each other and there will not always be the complete command present in the buffer, next chunk may come later. I was actually trying to use it this way: my $string = "5 abcde 2 fg 3 hij 6 klm"; while ($string =~ s/\s*(\d+)\s+(.{\1})//) { print "extracted: $1 $2\n"; } This way the while loop elegantly fails, when the pattern does not match and when the next data arrives, the check is made again. I was just trying to find the most elegant solution, but if it is not possible, I will create some work around, that is not the problem. But thank you all for your time and advice. Regards Honza Mach
smime.p7s
Description: S/MIME cryptographic signature