Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

returning variable from within regex

by pdotcdot (Acolyte)
on Aug 29, 2003 at 18:25 UTC ( [id://287786] : perlquestion . print w/replies, xml ) Need Help??

pdotcdot has asked for the wisdom of the Perl Monks concerning the following question:

Greeting Monks..Just a quickie i have the following code
elsif($var1=~/[$var2][a-z]{1}/{ #stuff; }
I would like to create a third variable based on the regex match generated above (ie the $var2 with the matched character a-z concatanated to the end). i suspect there is an easy answer but my brain is refusing to play ball. Thanks in advance, your servant pdotcodt.

Replies are listed 'Best First'.
Re: returning variable from within regex
by tcf22 (Priest) on Aug 29, 2003 at 18:34 UTC
    You can do this using $1..$n variables.
    elsif($var1=~/($var2[a-z]{1})/){ $var3 = $1; }

    NOTE: You also had a syntax error in the elsif statement. You were missing right paren.

      That {1} is not needed and the variable names need to be descriptive - @var2 and $var1 are not meaningful.

Re: returning variable from within regex
by roju (Friar) on Aug 29, 2003 at 19:34 UTC
    Can you maybe give an example of the data you're trying to match and the expected results? It's sounds like you want
    elsif($var1=~ /[$var2]([a-z])/) { #stuff with $1 }

    but it's hard to say.

Re: returning variable from within regex
by pdotcdot (Acolyte) on Aug 29, 2003 at 19:46 UTC
    example data:
    Original pattern: TMTMTMTMTETVTITITITX
    the X is removed earlier in the program as it never occurs in the larger sequence therefore when i search the pattern is never found.
    modified pattern:TMTMTMTMTETVTITITIT
    However... the character which X represents (which is any alphabetical character) must be found from the larger pattern.hence the a-z then i search through the larger string untill the pattern is found
    target pattern: TMTMTMTMTETVTITITIT[M]or[Q] etc
    and ideally remove the completed pattern which matched the regex so $var4 would equal TMTMTMTMTETVTITITITM or TMTMTMTMTETVTITITITQ apologies for being verbose.
Re: returning variable from within regex
by Fletch (Bishop) on Aug 29, 2003 at 18:38 UTC
    I would like to create a third variable based on the regex match generated above

    That sounds like you think you want a symref (perldoc perlreftut), but 99.95% of the time what you really mean is you want to use it as the key of a hash instead.

    Also see perldoc perlreftut for information on backreferences for determining what matched.

Re: returning variable from within regex
by pdotcdot (Acolyte) on Aug 29, 2003 at 19:15 UTC
    Currently i am working through those perldoc pages, cheers for the pointer. i do not need the variable as a hash key, i apologize if my question seemed to suggest that, i am really looking for a pattern in $var1 in which on the last letter changes to any character, hence the a-z section of the regex. i presume i have to reference the whole section:
    /($var2[a-z]{1})/
    Do reference within the regex? i tried the $n..$1 approach an it just returned blank lines( the correct number of blank lines for the match though...) sorry for the confusion pdotcot