in reply to Regex curly bracket ambiguity

To indicate where a variable ends in a regex, you surround the variable with {}: $string =~ m/${match}{$repeat}/;.

That keeps it from thinking you mean %match, but gives you m/Hello{2}/ (match "Hell", followed by two "o"s). To make the repeat apply to the whole $match, you need to group it: m/(?:${match}){$repeat}/, which makes the added {} unnecessary, leaving you with m/(?:$match){$repeat}/.

Replies are listed 'Best First'.
Re^2: Regex curly bracket ambiguity
by Anonymous Monk on Jun 21, 2007 at 18:08 UTC
    Thank you that was incredibly helpful.
    
    so now that I have regular brackets around (?:$match); what 
    do I do if I want to capture the whole thing?
    
    eg with:
    m/((?:$match){$repeat})/
    
    will $1 give me everything or just $match back?
    
    
      $1 will give you the whole match. (?:) is a non-capturing grouping, so it does not affect $1, etc. If you used regular parens around $match, it would be returned in $2.

      Caution: Contents may have been coded under pressure.
        If you used regular parens around $match, it would be returned in $2.
        That is, the last repetition would be returned in $2:
        $ perl -w $match = "ab+"; $string = "fooababbabbbar"; $string =~ /(($match){3})/ && print "\$1: $1 \$2: $2\n"; __END__ $1: ababbabbb $2: abbb