in reply to Backref in a regex quantifier

I don't think you will be able to do this in one step, you will probably have to do something like this:

if ( /^(\d+?)/ ) { my $len = $1; if ( /^$len(.{$len})/ ) { my $value = $1; # do something with $len and $value } }

We're not surrounded, we're in a target-rich environment!

Replies are listed 'Best First'.
Re^2: Backref in a regex quantifier
by webfiend (Vicar) on Jul 09, 2007 at 18:10 UTC

    Here's a marginally more clever version of your solution, which relies on the placeholder capabilities of a /g search. I mention it because I think it's a tiny bit more readable than the version you wrote. But ... you know ... personal preference and all that.

    use strict; use warnings; my $test_string = "3 xyz"; # Start a global search # It looks like the expected pattern includes a space. Let's put it he +re. if ( $test_string =~ m/^(\d+) /g ) { my $length = $1; # Pick up the search where our first match left off. if ( $test_string =~ m/\G(.{$length})/ ) { my $result = $1; # Do stuff with $result ... } }