dspivey has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|