the requirement I have is to check a string and based on particular set of chars either insert or replace with prefix string
$prefix = "DV1";
Following are my source $input strings:
SS7.ABCWT2.RSND.LTE1.QR
IT4.ABCET2.VCE2.QR
Y88.ABCNT2.MIM.EDR2.QR
9C5.ABCS.MIM.EDR2.QR
the first chars before first . can be of any length
but after the first . the chars ABC remain constant followed by any one character – these four chars will always be there in my input string.
after these 4 chars, the i/p string may have two alphanumeric chars – T2 in this case.
what needs to be done is check if $input has “T2” (can be any two alphanum chars) and if it has then replace those 2 chars with D1 (any two chars from $prefix)
if $input does not have “T2”, then insert $prefix
This can be done quite straightforwardly with a single substitution. This program demonstrates
The pattern looks for the sequence
.ABCfollowed by any non-dot character. The\Kprotects that part of the pattern from being changed. Then there may be two optional non-dot characters, followed by a dot. The replacement string isD1if the two optional characters were present, or the value of$prefixif notoutput