I have a fairly complex regexp, consisting of 9 match groups one of which has to match. I'm using copies of the exact same regexp in 4 different spots. All of them should match the same text. However, in one spot I want to access the match parameters as in $1 to $9, while in the other spots only one parameter ($3) and $`, $&, and $' are used.

How do I abstract a regexp so I only need to define and update it in one spot, but still be able to access the capture match groups anyway?

I tried storing the regexp pattern in a string variable then use that variable in the search, but I don't know how to correctly set the variable to a multi-line string (using a here document?) and use that for the pattern. Below is a dump of the working code, then the failing code and the error messages:

Working:
while (not $second and $linking =~ /(\bINT\s?[0-9A-Fa-f]{2}[Hh]? (?:\/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-F +a-f]{2,}[Hh]?)+ (?:\"[^"]+\")? ) |(\bINT\s?[0-9A-Fa-f]{2}[Hh]? (?:\"[^"]+\")? ) |(\b(?:E?A[XHL])=[0-9A-Fa-f]{2,}[Hh]? (?:\/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-F +a-f]{2,}[Hh]?)* (?:\"[^"]+\")? ) |(\#[0-9A-Z][0-9]{4}\b) |(\bMEM\s?[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1,4}[Hh] +? (?:\"[^"]+\")? ) |(\bMEM\s?[0-9A-Fa-fXx]{1,8}[Hh]? (?:\"[^"]+\")? ) |(\@[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1,4}[Hh]? (?:\"[^"]+\")? ) |(\bPORT\s?[0-9A-Fa-fXx]{1,4}[Hh]?-[0-9A-Fa-fXx]{1,4}[Hh +]? (?:\"[^"]+\")? ) |(\bPORT\s?[0-9A-Fa-fXx]{1,4}[Hh]? (?:\"[^"]+\")? ) /x) { my $intplus = $1; my $intonly = $2; my $regonly = $3; if (defined $regonly and not defined $int) { if (defined $link) { print_or_errorline("Entered reg only link without im +plicit INT"); return; } $maskhighlight .= " " x (length($`) + length($&)); $linking = $'; next; } my $table = $4; my $mem_16_16 = $5; my $mem_32 = $6; my $call = $7; my $portrange = $8; my $portsingle = $9; $maskhighlight .= " " x length $`;
Failing:
my $linkpattern = <<PATTERNEND; (\bINT\s?[0-9A-Fa-f]{2}[Hh]? (?:\/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-F +a-f]{2,}[Hh]?)+ (?:\"[^\"]+\")? ) |(\bINT\s?[0-9A-Fa-f]{2}[Hh]? (?:\"[^\"]+\")? ) |(\b(?:E?A[XHL])=[0-9A-Fa-f]{2,}[Hh]? (?:\/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-F +a-f]{2,}[Hh]?)* (?:\"[^\"]+\")? ) |(\#[0-9A-Z][0-9]{4}\b) |(\bMEM\s?[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1,4}[Hh] +? (?:\"[^\"]+\")? ) |(\bMEM\s?[0-9A-Fa-fXx]{1,8}[Hh]? (?:\"[^\"]+\")? ) |(\@[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1,4}[Hh]? (?:\"[^\"]+\")? ) |(\bPORT\s?[0-9A-Fa-fXx]{1,4}[Hh]?-[0-9A-Fa-fXx]{1,4}[Hh +]? (?:\"[^\"]+\")? ) |(\bPORT\s?[0-9A-Fa-fXx]{1,4}[Hh]? (?:\"[^\"]+\")? ) PATTERNEND while (not $second and $linking =~ /$linkpattern/x) { my $intplus = $1; my $intonly = $2; my $regonly = $3; if (defined $regonly and not defined $int) { if (defined $link) { print_or_errorline("Entered reg only link without im +plicit INT"); return; } $maskhighlight .= " " x (length($`) + length($&)); $linking = $'; next; } my $table = $4; my $mem_16_16 = $5; my $mem_32 = $6; my $call = $7; my $portrange = $8; my $portsingle = $9; $maskhighlight .= " " x length $`;
My program uses curses for a TUI, so I redirected stderr to a file to capture perl error messages:
Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unrecognized escape \s passed through at /home/[user]/proj/tractest/in +tlist.pl line 419. Unmatched ( in regex; marked by <-- HERE in m/ INTs?[0-9A +-Fa-f]{2}[Hh]? (?:/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-Fa +-f]{2,}[Hh]?)+ (?:"[^"]+")? ) |INTs?[0-9A-Fa-f]{2}[Hh]? (?:"[^"]+")? ) |(?:E?A[XHL])=[0-9A-Fa-f]{2,}[Hh]? (?:/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-Fa +-f]{2,}[Hh]?)* (?:"[^"]+")? ) |(#[0-9A-Z][0-9]{4) <-- HERE |MEMs?[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1, +4}[Hh]? (?:"[^"]+")? ) |MEMs?[0-9A-Fa-fXx]{1,8}[Hh]? (?:"[^"]+")? ) |(@[0-9A-Fa-fXx]{1,4}[Hh]?:[0-9A-Fa-fXx]{1,4}[Hh]? (?:"[^"]+")? ) |PORTs?[0-9A-Fa-fXx]{1,4}[Hh]?-[0-9A-Fa-fXx]{1,4}[Hh]? (?:"[^"]+")? ) |PORTs?[0-9A-Fa-fXx]{1,4}[Hh]? (?:"[^"]+")? ) / at /home/[user]/proj/tractest/intlist.pl line 447, <$array_lstff[... +]> line 197305.

In reply to Reusing a complex regexp in multiple spots, escaping the regexp by ecm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.