Hello, Monks. I'm not new to regex, but I can't seem to figure this substitution problem out.

Problem: I want to do multiple substitutions, but only on one part of the line. Quite simply, replace hyphen (-) with underscore (_) in the left-side (key) portion of this config data. Basically, everything before the colon should be subjected to the substitution of - to _. Because hyphens can exist in the value portion, I've had difficulty bailing out of the expression.

session-redis-hosts-fault-tolerant: "tyk-redis-1.gateways.svc.cluster. +local,tyk-redis-2..." message-center-db: "http://message-center-db-1b.message-center-db.svc. +cluster.local"

* I completed this using multiple steps, but there must be a 1-liner for this.
* I'm also doing in-line modification of multiple files.

This is the solution I came up with. It works, but how would I improve this using more advanced regex features? I've recently read about "backtracking control verbs", but I can't seem to figure out how to apply any of them to my problem.

perl -pi -e '($match) = m/^([^:]+)\:/; $ds = $match; $ds =~ s/\-/\_/g; + s/$match/$ds/' *.config

In reply to Repeated substitution on 1 side of a line only by dspivey

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.