Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Do you know where your variables are?
 
PerlMonks  

Re: Regular expressions: interpolate the variable in the value of the number of repetitions for the group

by rjt (Chaplain)
on Aug 03, 2013 at 01:40 UTC ( #1047671=note: print w/ replies, xml ) Need Help??


in reply to Regular expressions: interpolate the variable in the value of the number of repetitions for the group

The problem you want to solve is certainly solvable. However, the approach you want to take is going to be very ugly, if it's even possible at all. Here's what I would do instead:

s/(\d)//; say for (split /\n/)[0..$1-1];

However, you haven't mentioned what context this is in, and the data does seem highly contrived. What problem are you really trying to solve?

If you need to match an expression like this several times in a larger chunk of text, you'll need to split that text somehow, but you haven't given enough information for me to help you, there. (For example, split /(\d)/ first would give you an array of digits and strings to loop over.)


Comment on Re: Regular expressions: interpolate the variable in the value of the number of repetitions for the group
Select or Download Code
Re^2: Regular expressions: interpolate the variable in the value of the number of repetitions for the group
by 0day (Sexton) on Aug 03, 2013 at 05:47 UTC
    Heh... Thanks, but I want to do it the forces of the regular
    expression (without external code).

    Problem has practical examples (which are easily solved by
    external code), but I want to do it other way.

    Thanks.

      I do not know whether the solution proposed by rjt solves your problem, because you haven't decribed your problem and your data in sufficient details, but rjt's solution does not use any external code, but just Perl core functions and operators. And, BTW, the split function uses regular expressions.

      Heh... Thanks, but I want to do it the forces of the regular expression (without external code).

      The really funny thing is, all of the broken examples you cite as somehow desirable in the root node execute "external code" (which I take to mean non-regex Perl code), via the experimental features (?{ ... }) and (??{ ... }):

      0day's code:

      $binary_data =~ /(.)(.){(?{unpack 'H*', $1})}/; # ? /(.)(?{unpack 'H*', $1})(.){$^R}/; # ??? /(.)((??{'.' x $1}))/; /(\d)((??{'.*?\n' x $1}))/;

      how to assign a value aaaa\n of $3, bbbb\n -> $4 etc ?

      Short answer: you can't, unless you're OK with this:

      /(\d) ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? ([^\d]+?\n)? /x; printf "%d: <%s>\n", $_, eval '$'.$_ for 2..$1+1;

      But, no doubt you already thought of that. I want to help you, and to do that, I need a complete description of the actual problem you're trying to solve that you need our help with, as well as some real examples of input and expected output.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047671]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-04-19 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls