Use qr// to precompile a regex into a form that can be used instead of a regex on the right-hand-side of =~ and also interpolated into other regexen. Use named capture groups and %+ instead of $1..$9. Use (DEFINE) to reduce repitition within a single regex.

You didn't provide any sample input to really write a test program against, but here's a quick-and-dirty attempt to rewrite your regex that at least compiles. Quite a few more simplifications are probably possible, this is just to get you started.

my $regex = qr{ (?<intplus> (?&INT) (?&SOMETHING)+ (?&QUOT)? ) | (?<intonly> (?&INT) (?&QUOT)? ) | (?<regonly> \b(?:E?A[XHL])=[0-9A-Fa-f]{2,}[Hh]? (?&SOMETHING)* (?&QUOT)? ) | (?<table> \#[0-9A-Z][0-9]{4}\b) | (?<mem_16_16> \bMEM\s?(?&HEX4):(?&HEX4) (?&QUOT)? ) | (?<mem_32> \bMEM\s?[0-9A-Fa-fXx]{1,8}[Hh]? (?&QUOT)? ) | (?<call> \@(?&HEX4):(?&HEX4) (?&QUOT)? ) | (?<portrange> \bPORT\s?(?&HEX4)-(?&HEX4) (?&QUOT)? ) | (?<portsingle> \bPORT\s?(?&HEX4) (?&QUOT)? ) (?(DEFINE) (?<INT> \bINT\s?[0-9A-Fa-f]{2}[Hh]? ) (?<HEX4> [0-9A-Fa-fXx]{1,4}[Hh]? ) (?<QUOT> (?:\"[^"]+\") ) (?<SOMETHING> \/(?:E?[ABCD][XHL]|E?[SD]I|E?[SB]P|[DESC]S)=[0-9A-Fa-f]{2, +}[Hh]? ) ) }x;

In reply to Re: Reusing a complex regexp in multiple spots, escaping the regexp by haukex
in thread 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.