I want to compare two strings in TCL and replace the unmatched character with asterisk.
Core_cpuss_5/loop2/hex_reg[89]cpu_ip[45]_reg10/D[23] Core_cpuss_5/loop2/hex_reg[56]cpu_ip[12]_reg5/D[33]
Output Required : Core_cpuss_5/loop2/hex_reg[ * ]cpu_ip[ * ]_reg*/D[*]
I tried above using regsub but not working as expected.
foreach v {string1 string2} {
regsub {\[[0-9]+\]$} $v {[*]} v_modified
}
To replace the integers inside the square brackets with * (and also change reg10 and reg5 to reg*)
You had a couple problems in your code which I fixed:
{string1 string2}
to"$string1 $string2"
-all
to theregexp
command find all matches.$
from the regular expression because that only matches the final one.regsub
to change reg10 and reg5 to reg*.If you need a more general purpose solution, this will find a sequence of integers in each string and replace with a * if they are different:
The above will only work if the two strings are similar enough (like they each have the same number of integers in a similar order)