Search code examples
regextclregsub

Tcl regsub issue


I'm trying to write a TCL script to regsub a string within the contents of a file.

The string I'm searching for is a hexadecimal value denoted as X"12312312" I want to replace this string with another hexadecimal value, for example X"ABCDEF12"

As the hexadecimal value will change each time I run my script, I want the search to wildcard what's in between the quotes.

If I test my regsub as follows:-

regsub X"*" $content X"ABCDEF12" (where $content is the file I've read in) The regsub is succesfull, however when I inspect $content it has updated the hex value as follows:-

X"ABCDEF12"11223344"

can anybody please guide me as to what I'm doing wrong?

many thanks in advance


Solution

  • Your use of the * quantifier is wrong; you need to provide some atom that the quantifier applies to, e.g., . for any character; better:

    regsub -nocase -- {X"[A-Z0-9]*"} $content X"ABCDEF12"
    

    Also, you want to set the case sensitivity explicitly!


    Update

    As Donal suggests, you may turn on case-insensitive matching using an embedded option (?i) like:

    regsub -- {(?i)X"[A-Z0-9]*"} $content X"ABCDEF12"