Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Backref in a regex quantifier

by jasonk (Parson)
on Jul 09, 2007 at 13:13 UTC ( [id://625600]=note: print w/replies, xml ) Need Help??


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 ... } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-28 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found