How to make without exception uppercase and lowercase letters in tcl regsub?

163 Views Asked by At

I want to regsub without exception, so as not to double I do a regsub.

example before:
regsub -all "feat" $songtitle "" songtitle
regsub -all "Feat" $songtitle "" songtitle

I want a simple one line for regsub:
regsub -all "feat" $songtitle "" songtitle

It's a little inconvenient if there are many words that I want to regsub, I want it to be simple with no exceptions in the regsub, so that only one line of each word is regsub, not two lines for uppercase and lowercase letters. thanks

2

There are 2 best solutions below

0
On BEST ANSWER

You can specify the -nocase option to regsub to get it to ignore case when matching.

regsub -all -nocase {feat} $songtitle "" songtitle

You can also enable that mode of operation by putting the (?i) marker at the start of the RE:

regsub -all {(?i)feat} $songtitle "" songtitle

You probably should put some \y (a word boundary constraint) in that RE too, so that it doesn't change defeated into deed:

regsub -all {(?i)\yfeat\y} $songtitle "" songtitle

(Once you add either backslashes or square brackets to an RE, it's pretty much essential in Tcl that you put the RE in curly braces. Otherwise you end up using a disgustingly large number of backslashes…)

0
On

Also be aware of the string map command:

string map {feat "" Feat ""} $songtitle

Useful when you don't actually need regular expressions.