For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2006 > Re: Regexp $1









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Regexp $1
DJ Stunks

2006-12-22, 6:59 pm

albristow@gmail.com wrote:
> Hi there,
>
> I'm trying to create a regexp to validate a string where one of the
> characters in the string defines how long the string should be.
>
> e.g I want to validate a string like "3ABC" where the first character
> defines the length of the string.
>
> I was hoping it would be as easy as something like /(.).{$1}/ but this
> doesn't work because $1 must get substituted before it is set to the
> first character. Is there any way to do this with a regexp ?


If your requirements are more complex you could use a regular
expression and validate in a two step process:

sub is_ok{
my ($str) = @_;

return $str =~ m{^ (\d+) (.+) $}x && length $2 == $1;
}

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com