The rule with regexes is that non-alphanumeric characters (\W) lose their special meaning when escaped with a \, (\* is a litteral '*', \( is a litteral parenthesis etc...), while alphanumeric characters gain a special meaning when escaped. So \1, \2 etc... are reference to captured groups, \w means alphanumeric characters. This means that \x where x is a letter should always be considered to have a special meaning, even if this is not the case for the current version of perl, because such a meaning may be added in future versions. And yes, even interpolated variables are read using the regex syntax, and not taken litteraly.

\g is the start of a regex special meaning, referenced in perlreref:

\g1 or \g{1}, \g2 ... Matches the text from the Nth group
Since you don't have a number after you \g, this is, indeed, an incomplete pattern.

The solution is to remove its special meaning to \ (the escaping character), by adding another \ in front of it. The easiest way to do that is to use quotemeta which will leave all alphanumeric characters untouched, and escape all others (those that may have a special meaning). my $esc_bad_gitdir = quotemeta $bad_gitdit;
Or using the \Q shortcut: qr/Cannot find directory\(ies\): \Q$bad_gitdir/

Edit: there were typos everywhere...


In reply to Re: Unterminated \g... pattern in regex behaving badly on Windows by Eily
in thread Unterminated \g... pattern in regex behaving badly on Windows by jkeenan1

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.