Posted to tcl by dandyn at Mon Sep 23 19:39:08 GMT 2024view pretty

# regexp list below is created from a file. I have simulated the list below.
# Each elements might contain an replace arg at the end, separeted by space, in this case "replArg:\s".
set cleanDef [list \
{<[^>]*>} {^\s?DN\s(Direkt\s[\\-]|Debatt(?:\sRepliker)?\.)\s replArg:\s} \
{^\s?SvD(:?\:s)?\s[Mm]orgonrapport:?\s} {^\s?Direkt\:\sLIVECHATT\:\s replArg2:\s} \
{\s[-](\s[[:upper:]ÅÄÖ][^[:space:]]*){1,2}$} {\s\|\s.*}]

# example title to do "changes" to..
set dirty "DN Direkt - hello there, how are you?"

foreach def $cleanDef {

                # if an replace argument is included
                if {[llength $def] == "2"} {
                        set dirty [regsub -all [lindex [split $def] 0] $dirty [lindex [split $def] 1]]
                        set dirty [regsub -all "\\\\s" $dirty " "] 
                        puts $dirty

                } else {

                        set dirty [regsub -all $def $dirty ""]
                        puts $dirty
                }
                
}

puts "\nFinal\n$dirty"