Greetings Monks, I've been trying to figure out how to change the matching at the end of a string based on what I matched at the beginning. I've read up on conditional expressions and lookarounds, but I can't seem to figure out an elegant way to do this. As an example, take the nspI restriction enzyme recognition sequence:
/[AG]CATG[CT]/
By itself, it matches fine. However, I was wondering how I could force it to be palindromic; that is, if I match an 'A' with the first part then to force it to match a 'T' in the second. The best I've come up with so far is:
/[AG]CATG(?(?<=ACATG)T|C)/
which works ok. However, I was wondering if there is an easier/better way of doing this such that I don't have to try and match the entire sequence in the backreference and instead can just query the AG. Thank you for your time.

In reply to Question on conditional regular expressions/backreferences by sterben

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.